我有代码从facebook
获取我的好友列表代码是................
function getFriends() {
//if the person has not pressed login button
if(!loggedIn) {
loginFacebook();
}
document.getElementById("status").innerHTML = "Now loading your friends' id...";
//if the person is loggedIn
if(loggedIn) {
document.getElementById("friendBtn").disabled = "disabled";
FB.api("/me/friends",function(response){
friends = response["data"];
totalToBeLoaded = friends.length;
// addNewRow("Name","id");
loadid(friends.length);
});
}
}
response [“data”]函数以数组形式返回数据
{
"data": [
{
"name": "name 1",
"id": "123456781"
},
{
"name": "name 2",
"id": "123456782"
},
{
"name": "name 3",
"id": "123456783"
},
{
"name": "name 4",
"id": "123456784"
},
{
"name": "name 5",
"id": "123456785"
},
{
"name": "name 6",
"id": "123456786"
}
]
}
现在我需要从该响应中获取该用户的名称和ID [“data”]并打印出来......如何做到这一点?
答案 0 :(得分:2)
它是一个对象数组。您可以使用以下内容:
var list=response["data"];
for(var i=0;i<list.length;i++) {
addUser(list[i].name, list[i].id);
}
编辑:
甚至第一行也可以写成
var list=reponse.data;