我正在使用jQuery方法检索JSON中的某些信息,并遇到以下错误。
此外,我想使用 Chart.js 创建一个简单的图表,以显示我的json文件。
未捕获的TypeError:无法读取未定义的属性'0'
JavaScript
$.getJSON("geodata/passengerSub1_2.json", function(data) {
var labels = data.customers[0].amounts.map(function(e) {
return e[0];
});
var data = data.customers[0].amounts.map(function(e) {
return e[1];
});
var barChartSub1 = document.getElementById('barChart1').getContext('2d');
var chart = new Chart(barChartSub1, {
type: 'line',
data: {
labels: labels,
datasets: [{
backgroundColor: 'rgb(129, 198, 2228)',
borderColor: 'rgb(0, 150, 215)',
data: data
}]
},
options: {
responsive: 'true',
}
});
});
JSON
{
"customers": [
{
"first_name": "John",
"last_name": "Doe",
"account": "123456",
"period": "13th July - 13th August",
"due_date": "14th September",
"amounts": [
["January 2017", 121.23],
["February 2017", 145.23],
["March 2017", 55.12],
["April 2017", 78.58],
["May 2017", 89.13],
["June 2017", 45.78],
["July 2017", 90.22]
]
}
]
}