我試圖訪問我的嵌套數組的鍵和值是這樣的:如何訪問嵌套的JSON對象鍵和值
var obj = $.getJSON("mydata.json", function() {
console.log("load success");
});
Object.keys(obj[2].type).forEach(function(key) {
console.log(key, obj[key]);
});
但我得到一個錯誤。
這裏是JSON文件的結構:
{
"nodes": [
{
"nd": "nd1",
"cat": "cat1"
}
],
"links": [
{
"id": 100
}
],
"types": [
{
"type": "one",
"image": "image001"
},
{
"type": "two",
"image": "image002"
},
{
"type": "three",
"image": "image003"
}
]
}
我的目標是獲得的值的列表:
一二三
你什麼錯誤? –
'var vals = obj.types.map((x)=> x.image;});' – sed
這個工程對象 – Dimitri