2013-05-16 64 views
1

即時通訊目前請求通過JSON像這樣的Open Graph網址:Facebook的循環通過返回的JSON的Open Graph的要求

https://graph.facebook.com/me/friends?fields=email,likes,name,birthday,location,website&access_token=$token 

我的JSON請求是成功的,但我的問題是通過返回的數據實際上循環,使我可以在我的網頁上列出它。通過這個正確

{ 
    "data": [ 
     { 
     "name": "Name 1", 
     "birthday": "10/08/1983", 
     "location": { 
      "id": "110343502319180", 
      "name": "Copenhagen, Denmark" 
     }, 
     "id": "263901486" 
     }, 
     { 
     "name": "Name 2", 
     "birthday": "02/16/1982", 
     "location": { 
      "id": "110398488982884", 
      "name": "Reykjav\u00edk, Iceland" 
     }, 
     "id": "502533736" 
     }, 
    etc... 

如何循環任何建議:

我試着用下面的:

$('#goButton').click(function() { 

     //Call the JSON feed from the Open Social Graph. 
     $.getJSON("https://graph.facebook.com/me/friends?fields=email,likes,name,birthday,location,website&access_token=" + savedFbToken + "&callback=?", 
       //Once it is returned, do something with the JSON: 
       function (data) { 

       console.log(data); 
        //Clear out the placeholder 
        $('#Placeholder').html(""); 
        //Add some new data 
        $('#Placeholder').append("<h1>Name:</h1>" + data.name + ""); 
        $('#Placeholder').append("<span>Birthday: "+ data.birthday +"</span><br/>"); 
        $('#Placeholder').append("<span>Location: "+ data.location +"</span><br/>"); 
        $('#Placeholder').append("<span>Id: "+ data.id +"</span><br/><br/>"); 
       }); 
    }); 

格式返回這個樣子的?

回答

2

data實際上包含的對象與對象也稱爲數據數組,所以你會通過

$('#Placeholder').html(""); 

for(var i = 0; i < data.data.length; i++){ 
//Add some new data 
$('#Placeholder').append("<br />"); 
$('#Placeholder').append("<h1>Name:</h1>" + data.data[i].name + ""); 
$('#Placeholder').append("<span>Birthday: "+ data.data[i].birthday +"</span><br/>"); 
$('#Placeholder').append("<span>Location: "+ data.data[i].location +"</span><br/>"); 
$('#Placeholder').append("<span>Id: "+ data.data[i].id +"</span><br/><br/>"); 
} 
+0

輝煌需要循環 - 這很簡單!感謝一堆:) – user1231561