我正在尝试从 javascript 中读取 json。我已经能够读取该文件,但由于其结构,我在从该文件中检索信息时遇到了一些问题。我不知道某些级别的名称...
这是一个例子:
{
"zona": [{
"zona1": [{
"lon": "-3.841867446899414",
"lat": "43.466002139041116"
}, {
"lon": "-3.838176727294922",
"lat": "43.466002139041116"
}, {
"lon": "-3.838348388671875",
"lat": "43.46432016607394"
}, {
"lon": "-3.843669891357422",
"lat": "43.46276274196949"
}]
}, {
"zona2": [{
"lon": "-3.832254409790039",
"lat": "43.462513550389424"
}, {
"lon": "-3.8265037536621094",
"lat": "43.46375949801845"
}, {
"lon": "-3.8254737854003906",
"lat": "43.461641371770504"
}, {
"lon": "-3.829936981201172",
"lat": "43.46002157809642"
}, {
"lon": "-3.832683563232422",
"lat": "43.461142978339005"
}]
}]
}
我试图做这样的事情,但我觉得我有点天真,哈哈
function fun() {
var zona;
$.getJSON('listaPuntosZona.json', function(data) {
for (i = 0; i < data.zona.length; i++) {
zonaX = 'zona' + (i + 1);
alert(data.zona[i].zonaX.length);
}
});
我已经阅读了 foreach 循环,但我不理解它们,所以我无法为它们编写代码。有没有办法按照我的想法去做?第三个参数,ZonaX 从 zona1 到 zonaN 但 1,2,3....N,总是 +1。