在解析nested json
文件时,我发现以下错误。
Cannot read property 'beginTimeSeconds' of undefined
嵌套Json:
{"facets":[{"total":{"results":[{"count":448}],"beginTimeSeconds":0,"endTimeSeconds":0,"inspectedCount":448},"timeSeries":[{"results":[{"count":38}],"beginTimeSeconds":1491300260,"endTimeSeconds":1491301160,"inspectedCount":38},{"results":[{"count":28}],"beginTimeSeconds":1491301160,"endTimeSeconds":1491302060,"inspectedCount":28},{"results":[{"count":35}],"beginTimeSeconds":1491302060,"endTimeSeconds":1491302960,"inspectedCount":35},{"results":[{"count":34}],"beginTimeSeconds":1491302960,"endTimeSeconds":1491303860,"inspectedCount":34},{"results":[{"count":39}],"beginTimeSeconds":1491303860,"endTimeSeconds":1491304760,"inspectedCount":39},{"results":[{"count":37}],"beginTimeSeconds":1491304760,"endTimeSeconds":1491305660,"inspectedCount":37},{"results":[{"count":36}],"beginTimeSeconds":1491305660,"endTimeSeconds":1491306560,"inspectedCount":36},{"results":[{"count":36}],"beginTimeSeconds":1491306560,"endTimeSeconds":1491307460,"inspectedCount":36},{"results":[{"count":45}],"beginTimeSeconds":1491307460,"endTimeSeconds":1491308360,"inspectedCount":45},{"results":[{"count":38}],"beginTimeSeconds":1491308360,"endTimeSeconds":1491309260,"inspectedCount":38},{"results":[{"count":45}],"beginTimeSeconds":1491309260,"endTimeSeconds":1491310160,"inspectedCount":45},{"results":[{"count":37}],"beginTimeSeconds":1491310160,"endTimeSeconds":1491311060,"inspectedCount":37}],"name":"b20"}]}
编写如下代码:
var json = {above--nested--json}
$.each(json.facets, function(i, item) {
var obj = json.facets[i];
name = json.facets[i].name;
console.log(json.facets[0]);
data.addColumn('string', name);
$.each(json.facets[i], function(j, item) {
console.log('Time Series : '+json.facets[i]);
curtime = json.facets[i].timeSeries[j].beginTimeSeconds;
curcount = json.facets[i].timeSeries[j].inspectedCount;
curname = name + ' : ' + curcount;
});
data.addRows([ [ new Date(curtime * 1000), curname ] ]);
});
当我打印时,console.log(json.facets[0].timeSeries[0].beginTimeSeconds)
正在打印,但json.facets[i].timeSeries[j].beginTimeSeconds
根本不起作用。
非常感谢任何帮助。
提前致谢。
答案 0 :(得分:1)
您的代码中的问题是您在各种不同的对象上调用$.each
- 而不是数组。因此,您似乎期望像j
这样的变量保存包含元素索引的整数,而实际上它们在对象中保存属性的key
。
要解决此问题,您可以使用$.each
在处理函数参数中提供的对象的引用来简化代码,如下所示:
$.each(json.facets, function(i, facet) {
name = facet.name;
data.addColumn('string', name);
$.each(facet.timeSeries, function(j, timeSeries) {
curtime = timeSeries.beginTimeSeconds;
curcount = timeSeries.inspectedCount;
var curname = name + ' : ' + curcount;
// do something with curname here...
});
data.addRows([
[new Date(curtime * 1000), curname]
]);
});
var json = {
"facets": [{
"total": {
"results": [{
"count": 448
}],
"beginTimeSeconds": 0,
"endTimeSeconds": 0,
"inspectedCount": 448
},
"timeSeries": [{
"results": [{
"count": 38
}],
"beginTimeSeconds": 1491300260,
"endTimeSeconds": 1491301160,
"inspectedCount": 38
}, {
"results": [{
"count": 28
}],
"beginTimeSeconds": 1491301160,
"endTimeSeconds": 1491302060,
"inspectedCount": 28
}, {
"results": [{
"count": 35
}],
"beginTimeSeconds": 1491302060,
"endTimeSeconds": 1491302960,
"inspectedCount": 35
}, {
"results": [{
"count": 34
}],
"beginTimeSeconds": 1491302960,
"endTimeSeconds": 1491303860,
"inspectedCount": 34
}, {
"results": [{
"count": 39
}],
"beginTimeSeconds": 1491303860,
"endTimeSeconds": 1491304760,
"inspectedCount": 39
}, {
"results": [{
"count": 37
}],
"beginTimeSeconds": 1491304760,
"endTimeSeconds": 1491305660,
"inspectedCount": 37
}, {
"results": [{
"count": 36
}],
"beginTimeSeconds": 1491305660,
"endTimeSeconds": 1491306560,
"inspectedCount": 36
}, {
"results": [{
"count": 36
}],
"beginTimeSeconds": 1491306560,
"endTimeSeconds": 1491307460,
"inspectedCount": 36
}, {
"results": [{
"count": 45
}],
"beginTimeSeconds": 1491307460,
"endTimeSeconds": 1491308360,
"inspectedCount": 45
}, {
"results": [{
"count": 38
}],
"beginTimeSeconds": 1491308360,
"endTimeSeconds": 1491309260,
"inspectedCount": 38
}, {
"results": [{
"count": 45
}],
"beginTimeSeconds": 1491309260,
"endTimeSeconds": 1491310160,
"inspectedCount": 45
}, {
"results": [{
"count": 37
}],
"beginTimeSeconds": 1491310160,
"endTimeSeconds": 1491311060,
"inspectedCount": 37
}],
"name": "b20"
}]
}
$.each(json.facets, function(i, facet) {
name = facet.name;
//data.addColumn('string', name);
$.each(facet.timeSeries, function(j, timeSeries) {
curtime = timeSeries.beginTimeSeconds;
curcount = timeSeries.inspectedCount;
curname = name + ' : ' + curcount;
console.log(curname);
});
/*
data.addRows([
[new Date(curtime * 1000), curname]
]);
*/
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;