I am new to jQuery and Java script. I need to parse a JSON file that has arrays, I use this code:
$.getJSON('mat.json', function(data) {
var items = [];
$.each(data, function(key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>', {'class': 'my-new-list', html: items.join('')}).appendTo('body');
});
For this JSON file:
{
"@file_name": "materials",
"materials": [{
"@site_name_English": "N/A",
"@site_name_Spanish": "N/A",
"@site_number": "1",
"zoom": [{
"@zoom_name_English": "Main Reservoir",
"@zoom_name_Spanish": "Depósito principal",
"@zoom_number": "1",
"icon": [
{
"@icon_name": "Info Icon 1",
"@icon_pin": "1"
},
{
"@icon_name": "Info Icon 2",
"@icon_pin": "2"
}
]
}]
}]
}
But my result is:
materials
[object Object]
How can I change my code so I will get the objects also when the loop meets them?
li
id and content, respectively? – Raibaz Aug 8 at 8:05