我是 jQuery 和 Java 脚本的新手。我需要解析一个包含数组的 JSON 文件,我使用以下代码:
$.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');
});
对于这个 JSON 文件:
{
"@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"
}
]
}]
}]
}
但我的结果是:
materials
[object Object]
如何更改我的代码,以便在循环遇到对象时也能获得对象?