解析 JSON 并加载到数组中
Parsing JSON and loading into array
JSON-https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo
我正在尝试从上面的 link 中获取 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] returns 未定义。关于为什么会发生这种情况的任何想法?
谢谢!
此时已经JSON。正如帕特里克提到的,getJSON 自动解析 json。您将需要使用数组括号表示法访问它,因为 属性 名称中有空格。
var timeSeries = data['Time Series (Daily)'];
原因是在each
循环中,value
不是列表而是对象。要访问它,您必须使用密钥获取它。
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
JSON-https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo
我正在尝试从上面的 link 中获取 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] returns 未定义。关于为什么会发生这种情况的任何想法?
谢谢!
此时已经JSON。正如帕特里克提到的,getJSON 自动解析 json。您将需要使用数组括号表示法访问它,因为 属性 名称中有空格。
var timeSeries = data['Time Series (Daily)'];
原因是在each
循环中,value
不是列表而是对象。要访问它,您必须使用密钥获取它。
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