2011-02-10 48 views
0

好吧,我相當新的jQuery JSON的東西。我正試圖訪問下面的json輸出中的actions > name節點。它看起來像「行動」是「數據」數組內的數組。通過使用fb.message,我可以正確訪問「消息」部分,並且我可以通過fb.from.name訪問「from」下的「name」節點,從而獲得json結果。那麼我將如何訪問「動作」下的「名稱」節點array

感謝您的任何幫助!

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

回答

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); 
}); 
+0

謝謝,這正是我所需要的!此外,如果您碰巧知道......我正在循環訪問數據數組中的所有對象。如果返回undefined,我怎麼能從循環中刪除一個?還是有一個簡單的方法來做到這一點?讓我知道你是否需要代碼示例。 – RyanPitts 2011-02-10 04:33:13

1

JSON對象只是一般的JavaScript物件,訪問他們,你通常會以同樣的方式,獲得i日數據項的j th操作可以使用下面的代碼。要獲得每一個的第一個硬編碼i=j=0

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

謝謝...您和其他回覆幫助解決了我的問題!我選擇與他一起去,但我仍然會給你一個觀點。 – RyanPitts 2011-02-10 04:31:42

0

您可以在陣列中訪問的第一個元素:

fb.actions[0].name