好的,我是jQuery JSON的新手。我正在尝试访问以下json输出中的actions > name
节点。看起来“actions”是“data”数组中的一个数组。我获得json结果的方式我可以使用fb.message
正确访问“消息”部分,我可以使用fb.from.name
访问“from”下的“name”节点。那么我将如何访问“actions” array 下的“name”节点?
感谢您的帮助!!
CODE:
({ "data": [{
"id": "1755670903_1287007912714",
"from": {
"name": "LifeBridge Church",
"id": "1755670903"
},
"message": "Due to weather, church service tomorrow is canceled. See you next week!",
"icon": "http://photos-d.ak.fbcdn.net/photos-ak-snc1/v27562/23/2231777543/app_2_2231777543_9553.gif",
"actions": [
{
"name": "\u0040lifebridgeTX on Twitter",
"link": "http://twitter.com/lifebridgeTX?utm_source=fb&utm_medium=fb&utm_campaign=lifebridgeTX&utm_content=33711605665505280"
}
],
"type": "status",
"application": {
"name": "Twitter",
"id": "2231777543"
},
"created_time": "2011-02-05T02:20:48+0000",
"updated_time": "2011-02-05T02:20:48+0000"
},
{
"id": "1755670903_1281724020620",
"from": {
"name": "LifeBridge Church",
"id": "1755670903"
},
"message": "Service tonight at 5:30... meet us at Eddins Elementary school in McKinney. http://see.sc/8l4Cwo || Praying that God is greatly glorified!",
"icon": "http://photos-d.ak.fbcdn.net/photos-ak-snc1/v27562/23/2231777543/app_2_2231777543_9553.gif",
"actions": [
{
"name": "\u0040lifebridgeTX on Twitter",
"link": "http://twitter.com/lifebridgeTX?utm_source=fb&utm_medium=fb&utm_campaign=lifebridgeTX&utm_content=31388610301272065"
}
],
"type": "status",
"application": {
"name": "Twitter",
"id": "2231777543"
},
"created_time": "2011-01-29T16:30:03+0000",
"updated_time": "2011-01-29T16:30:03+0000"
}]
});
其他问题的后续代码:
$.each(json.data,function(i,fb){
if (!fb.message) continue;
facebookPost += '<li class="ui-li ui-li-static ui-btn-up-c" role="option" data-theme="c">' + fb.message + ' <br /><span class="via">' + fb.created_at + ' via ' + fb.actions[0].name + '</span></li>';
$('#facebookPosts li:first').after(facebookPost);
});
答案 0 :(得分:1)
JSON对象只是普通的javascript对象,以通常的方式访问它们,以获得i
数据项的j
动作,您可以使用以下代码。要获得每个中的第一个,您只需硬编码i=j=0
。
jsonResponse.data[i].actions[j].name
答案 1 :(得分:1)
我假设当你说fb
时fb = data[0]
。然后:
fb.actions; // Gives actions ARRAY
fb.actions[0]; // Gives first OBJECT in actions ARRAY
fb.actions[0].name; // Gives the name VALUE of the first OBJECT in actions ARRAY
根据评论中的问题,在值不存在时跳过项目:
$.each(json.data,function(i,fb){
if (!fb.message) return true; // true to keep going, false to quit immediately
facebookPost += '<li class="ui-li ui-li-static ui-btn-up-c" role="option" data-theme="c">' + fb.message + ' <br /><span class="via">' + fb.created_at + ' via ' + fb.actions[0].name + '</span></li>';
$('#facebookPosts li:first').after(facebookPost);
});
答案 2 :(得分:0)
您可以访问数组中的第一个元素:
fb.actions[0].name