我有一个来自站点https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&outputsize=full&apikey=demo的json
看起来像
{
"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,不知道如何遍历这些不同的日期。
console.log(data['TimeSeries (Daily)']['2018-07-03']);
这是我访问日期的方式,但这是我要遍历每个日期的单个日期。
答案 0 :(得分:1)
这将为您提供所需的输出
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']);
}
更新
如果您需要访问日期并希望将其放在单独的数组中
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)
答案 1 :(得分:0)
浏览“ TimeSeries(每日)”对象的键。例如,
for(date in obj) {
console.log(obj[date]);
}
要访问每个日期的数据,只需添加另一个循环即可。
答案 2 :(得分:0)
您可以使用for in循环获取所需的输出
let myList = data['TimeSeries (Daily)'];
for (var index in myList)
{
console.log(myList[index]);
}
致谢
答案 3 :(得分:0)
如果您使用的是现代JavaScript引擎或babel
之类的编译器,则可以使用Object.values
和Object.entries
来完成:
Object.values(myData["Time Series (Daily)"]).forEach(function (date) {
Object.entries(date).forEach(function([period, value]) {
console.log(period, ':', value);
})
})
这将在您的控制台中记录以下内容:
1. open : 100.4800
2. high : 100.6300
3. low : 98.9400
// ....
答案 4 :(得分:0)
恕我直言,这就是您要寻找的:
data = {
"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"
}
}
}
Object.keys(data["Time Series (Daily)"]).forEach(date => console.log(date))
答案 5 :(得分:0)
这是最简单的方法...
$
尝试在同一域中以防止跨域问题