1

我目前通过 JSON 请求 Open Graph url,如下所示:

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

我的 JSON 请求成功,但我的问题是实际循环返回的数据,以便我可以在我的页面上列出它。

我尝试过以下方法:

$('#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/>");
              });
    });

返回的格式如下所示:

{
   "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...

关于如何正确循环的任何建议?

4

1 回答 1

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/>");
}
于 2013-05-16T22:16:03.687 回答