阅读JSON对象的jQuery
问题描述:
我有这样的JSON对象:阅读JSON对象的jQuery
"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
将失败。您的JSON的
答
您的JSON定义了一个名为“Master”的键。尝试:
alert(obj.Master.nodes.items[0].icon);
+0
试过,我得到:'不能读取未定义的属性0# –
+0
@ free4ride得到它的权利 - 项目是在节点的事件键下。 – mmigdol
答
确保你有有效的JSON,你可以试试这个:http://www.freeformatter.com/json-formatter.html –