I am trying to read a json from javascript. I already am able to read the file but I have some problems retrieving info from that file because of its structure. I don’t know the name of some levels…
This is an example:
{
"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"
}]
}]
}
I tried to do something like this but I think I was a little bit innocent lol
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);
}
});
I have read about foreach loops but I don’t understand them so I can’t write a code for them. Is there a way to do as I am thinking?
Third parameter, ZonaX goes from zona1 to zonaN but 1,2,3….N, always +1.
To explore unknown JSON/object structures you may call the function walk(data) as
… function(data) {walk(data); …