开发者

Trouble with $.getJSON - looks like arrays inside of arrays

Ok, i'm fairly new to jQuery JSON stuff. I'm trying to access the actions > name node in the below json output. It looks like "actions" is an array inside of the "data" array. The way i am getting the json results i can access the "message" portion correctly by using fb.message and i can access the "name" node under "from" with fb.from.name. So how would i access the "name" node under the "actions" array?

Thanks for any help!!

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": "\u0040life开发者_如何学CbridgeTX 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"
      }]
});

Follow-up code for additional questions:

$.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);
});


I'm assuming when you say fb, that fb = data[0]. Then:

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

From a question in the comments, to skip over items when a value doesn't exist:

$.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);
});


JSON objects are just normal javascript objects, access them the same way you would normally, to get the ith data item's jth action you could use the following code. to get the first of each you could just hardcode i=j=0.

jsonResponse.data[i].actions[j].name


You can access the first element in the array:

fb.actions[0].name
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜