在给定的函数中,我传递的数据是json格式,如下所示
[{"Date":"2014-12-12 18:52:10","TTC":"0"},{"Date":"2014-12-12 18:52:10","TTC":"0"},{"Date":"2014-12-12 18:53:10","TTC":"0"},{"Date":"2014-12-12 18:53:10","TTC":"0"},{"Date":"2014-12-12 19:02:11","TTC":"0"}]
JSON数组太长了,我想将Date存入日期数组,将TTC存储到ttc中,请帮助解析。
function drawChart(data){
console.log(data);
var date = [];
var ttc = [];
for(var i=0; i<data.length; i++){
date.push(data[i]['Date']);
console.log(date);
ttc.push(parseInt(data[i]["TTC"]));
//console.log(ttc);
}
}
答案 0 :(得分:1)
以这种方式尝试:
var data = [{
"Date": "2014-12-12 18:52:10",
"TTC": "0"
}, {
"Date": "2014-12-12 18:52:10",
"TTC": "0"
}, {
"Date": "2014-12-12 18:53:10",
"TTC": "0"
}, {
"Date": "2014-12-12 18:53:10",
"TTC": "0"
}, {
"Date": "2014-12-12 19:02:11",
"TTC": "0"
}];
function drawChart(data) {
var date = [];
var ttc = [];
for (var i = 0; i < data.length; i++) {
date.push(data[i]['Date']);
ttc.push(parseInt(data[i]["TTC"]));
}
return {
date: date,
ttc: ttc
};
}
document.body.innerHTML = JSON.stringify(drawChart(data));
您需要返回一个包含数组的对象
答案 1 :(得分:0)
为了像对象一样访问它,你必须像这样解析它:
obj = JSON.parse(data);