我有这个 JSON 对象:
"Master": {
"nodeType": "Test",
"label": "Master",
"nodes": {
"event": {
"nodeType": "Test",
"label": "Test",
"itemTemplate": {
"label": "Test",
"properties": {
"icon": {
"label": "Test",
"description": "Test",
"valueType": "STRING",
"value": ""
}
}
},
"items": [
{
"icon": "test 2",
},
{
"icon": "test 1",
}
]
}
}
我想访问items
部分。我尝试过以下方法:
var obj = jQuery.parseJSON(json_object); //json object
alert(obj.nodes.items[0].icon); //Uncaught TypeError: Cannot read property 'items' of undefined
最佳答案
var items;
items = obj.Master.nodes.event.items;
alert(items[0].icon);
items
结果是一个数组
请注意,您的 json 不正确,因为缺少前导 { 和两个尾随 }
json_object = '{ "Master": {
//
//
//
} }';
如果不正确jQuery.parseJSON
将会失败。
关于javascript - 使用 jQuery 读取 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21713432/