I have a problem with getting some data from JSON format. I already got most of the data, but there is data which is an array inside of an array and it's more complicated. I can't seem to find out what should I do.
The JSON file is like that:(that's a part of it - i can't change it)
{
"smth": [
{
"part": {
"d": 295.15
},
"rock": [
{
"description": "heavy",
"icon": "22sd"
}
],
"song": 10
}
{
"part": {
"d": 295.15
},
"rock": [
{
"description": "soft",
"icon": "33sd"
}
],
"song": 10
}
]
}
My code, a part which doesn't work as I want:
var icon=[];
var desc=[];
$.getJSON(url, function(json) {
$.each(json.smth, function() {
$.each(this.rock, function() {
for (var i in json.smth){
main[i] = this.description;
icon[i] = this.icon;
}
});
});
document.getElementById('icon1').src='..//img/w/'+icon[0]+'.png';
document.getElementById('icon2').src='..//img/w/'+icon[1]+'.png';
document.getElementById('icon3').src='..//img/w/'+icon[2]+'.png';
document.getElementById('icon4').src='..//img/w/'+icon[3]+'.png';
document.getElementById('icon5').src='..//img/w/'+icon[4]+'.png';
document.getElementById('main1').innerHTML=desc[0];
document.getElementById('main2').innerHTML=desc[1];
document.getElementById('main3').innerHTML=desc[2];
document.getElementById('main4').innerHTML=desc[3];
document.getElementById('main5').innerHTML=desc[4];
});
I need to get data as Array. But I can't wrote that part like: this[i].description
.
The other data worked well like this:
rain[i]=json.smth[i].part.d;
There I didn't need to use
$.each(json.list, function()
Also, I don't know if it's somehow possible for the last 10 rows in my code to use a loop..
Does anyone have any suggestions? I'd be really grateful..
json.list
? Do you just want topush
each item from therock
array into another array? – Matt Burland Jun 6 at 19:00json.list
? Or rather, what are you trying to do withthis.description
andthis.icon
? – Jim H. Jun 6 at 19:12main[i]=json.smth.rock[i].description;
but the result was that I didn't get the value.. the wordundefined
appeared there... that's why I use now$.each(json.smth, function()
and values appear but if I use this I can't add [i] there.. so result is that values appear in html but there is everywhere only the last value of json array.. – PureSoul Jun 6 at 19:22