gpt4 book ai didi

javascript - 解析 JSON 并加载到数组中

转载 作者:行者123 更新时间:2023-12-03 03:59:33 25 4
gpt4 key购买 nike

JSON-https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo

我试图从上面的链接中获取 JSON 并将其放置为以下格式(日期、开盘价、最高价、最低价、收盘价)...

[
[1277424000000,38.58,38.61,37.97,38.10],
[1277683200000,38.13,38.54,37.79,38.33],
[1277769600000,37.73,37.77,36.33,36.60],
[1277856000000,36.67,36.85,35.72,35.93],
]

日期不需要必须是纪元时间。

我的代码....

$.getJSON('https://www.alphavantage.co/query?
function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo', function(data) {
//Get the time series data
var timeseries = data['Time Series (Daily)']
var ohlcarray = [];
//Loop through each time series and convert it to JSON format
$.each(timeseries, function(key, value) {
var ohlcdata=[];
ohlcdata[0]=value[0];//date
ohlcdata[1]=value[1];//open
ohlcdata[2]=value[2];//high
ohlcdata[3]=value[3];//low
ohlcdata[4]=value[4];//low
ohlcarray.push(ohlcdata);
});
console.log(ohlcarray[0]);//test if worked properly
});

输出....

[undefined, undefined, undefined, undefined, undefined]

值[x]返回未定义。关于为什么会发生这种情况有什么想法吗?

谢谢!

最佳答案

原因是在each循环中,value不是一个列表而是一个对象。要访问它,您必须使用 key 来获取它。

ohlcdata[0] = key; //date
ohlcdata[1] = value['1. open']; //open
ohlcdata[2] = value['2. high']; //high
ohlcdata[3] = value['3. low']; //low
ohlcdata[4] = value['4. close']; //low

https://jsfiddle.net/koralarts/7c2fkf93/2/

关于javascript - 解析 JSON 并加载到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44786738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com