从 JSON 解析和获取数据 [英] Parsing and getting data from JSON

查看:47
本文介绍了从 JSON 解析和获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自站点的 json https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&outputsize=full&apikey=demo

I have a json from site https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&outputsize=full&apikey=demo

看起来像

{
"Meta Data": {
    "1. Information": "Daily Prices (open, high, low, close) and Volumes",
    "2. Symbol": "MSFT",
    "3. Last Refreshed": "2018-07-03",
    "4. Output Size": "Full size",
    "5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
    "2018-07-03": {
        "1. open": "100.4800",
        "2. high": "100.6300",
        "3. low": "98.9400",
        "4. close": "99.0500",
        "5. volume": "14670275"
    },
    "2018-07-02": {
        "1. open": "98.1000",
        "2. high": "100.0600",
        "3. low": "98.0000",
        "4. close": "100.0100",
        "5. volume": "19564521"
    }
}
}

您可以在时间序列(每日)"中看到不同的日期对象.我想访问每个日期中的所有键并将数据放入数组中.我正在使用 javascript,但不知道如何遍历这些不同的日期.

You can see in "Time Series (Daily)" there are different date objects. I want to access all the keys in each date and want to put data in array. I am using javascript and have no idea how to iterate through these different dates.

        console.log(data['TimeSeries (Daily)']['2018-07-03']);

这是我访问日期的方式,但这是我想遍历每个日期的单个日期.

This is how i accessed date, but this is single date i want to iterate through each.

推荐答案

这会给你想要的输出

var tSeries=data['Time Series (Daily)'];
for(var tempData in tSeries)
{   
    console.log(tSeries[tempData]);
    console.log(tSeries[tempData]['1. open']);
    console.log(tSeries[tempData]['2. high']);
}

更新

如果您需要访问日期并将它们放在单独的数组中

If you need to access dates and wants them in separate Array

var tempDataStore = [];
var tSeries=data['Time Series (Daily)'];
for(var tempData in tSeries)
{   
    // Here You get your Date
    console.log("Your Date - " + tempData);
    console.log(tSeries[tempData]);
    console.log(tSeries[tempData]['1. open']);
    console.log(tSeries[tempData]['2. high']);
    tempDataStore.push({"Date":tempData,"open":tSeries[tempData]['1. open']})
}

console.log(tempDataStore)

这篇关于从 JSON 解析和获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆