0
我被這個問題困住了。我有下面的嵌套json。使用javascript從嵌套的json文件中提取數據
{
"current": {
"timeSeries": [
{
"results": [
{
"count": 426
}
],
"beginTimeSeconds": 1490928320,
"endTimeSeconds": 1490930120,
"inspectedCount": 426
},
{
"results": [
{
"count": 510
}
],
"beginTimeSeconds": 1490930120,
"endTimeSeconds": 1490931920,
"inspectedCount": 510
}
]
},
"previous": {
"timeSeries": [
{
"results": [
{
"count": 426
}
],
"beginTimeSeconds": 1490928320,
"endTimeSeconds": 1490930120,
"inspectedCount": 426
},
{
"results": [
{
"count": 510
}
],
"beginTimeSeconds": 1490930120,
"endTimeSeconds": 1490931920,
"inspectedCount": 510
}
]
}
}
而我試圖從上面的json中提取數據,並使用谷歌可視化API顯示在網頁上。
我試過下面的js,它沒有工作。
function drawChart() {
var jsonData = $.ajax({
url: "https://api.myjson.com/bins/bay0v",
dataType: "json",
async: false
}).responseText;
var options = {
title : 'Sample json line chart',
curveType : 'function',
};
//var obj = JSON.parse(jsonData);
console.log(jsonData);
console.log(jsonData.current.timeSeries[0].beginTimeSeconds);
var options = {
hAxis: {
title: 'Time',
textStyle: {
color: '#01579b',
fontSize: 20,
fontName: 'Arial',
bold: true,
italic: true
},
titleTextStyle: {
color: '#01579b',
fontSize: 16,
fontName: 'Arial',
bold: false,
italic: true
}
},
vAxis: {
title: 'Popularity',
textStyle: {
color: '#1a237e',
fontSize: 24,
bold: true
},
titleTextStyle: {
color: '#1a237e',
fontSize: 24,
bold: true
}
},
colors: ['#a52714', '#097138']
};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, {width: 1400, height: 360});
}
我在瀏覽器控制檯中看到下面的輸出。
Uncaught TypeError: Cannot read property 'timeSeries' of undefined
有人可以幫我解決這個問題嗎?
在此先感謝。
'異步:FALSE'已被棄用。您需要編寫一個(異步)回調。 – trincot
感謝@trincot,但是我沒有閱讀jsonData對象的問題。當我打印時,它打印整個json文件。你有什麼建議來編寫異步回調? – harshavmb