我无法访问DATA数组中的数字,这将是COLUMNS中提到的ID。
我的JSON如下:
{
"COLUMNS": [
"ID",
"DESCRIPTION",
"DATE"
],
"DATA": [
[
53,
"Try to do something test123",
"September, 05 2017 00:00:00 +0100"
]
]
}
我目前的尝试是这样,但是有了这个,我得到了全部三个要素
var jsonLength= JSON.DATA.length;
for (dataIndex = 0; dataIndex < jsonLength; dataIndex++) {
var dataLength= JSON.DATA[dataIndex].length;
for (noteIndex = 0; noteIndex < dataLength; noteIndex++) {
alert(JSON.DATA[dataIndex]);
}
}
最佳答案
您的代码几乎是正确的,只是在2D DATA
数组上缺少第二个索引访问器。您可以从循环中使用noteIndex
递增变量:
var JSON = {
"COLUMNS": [ "ID", "DESCRIPTION", "DATE" ],
"DATA": [
[ 53, "Try to do something test123", "September, 05 2017 00:00:00 +0100" ]
]
}
var jsonLength = JSON.DATA.length;
for (dataIndex = 0; dataIndex < jsonLength; dataIndex++) {
var dataLength = JSON.DATA[dataIndex].length;
for (noteIndex = 0; noteIndex < dataLength; noteIndex++) {
console.log(JSON.DATA[dataIndex][noteIndex]); // note the additional [] here
}
}