如何从FB.api返回的对象中读取属性数据?
|
我有以下代码实现,可以在用户发表评论时捕获事件。它可以正确触发,但是问题是我不知道如何解析传递给我的回调函数的对象。
<script>
window.fbAsyncInit = function () {
FB.init({ appId: \'<myAppId>\', status: true, cookie: true, xfbml: true });
FB.Event.subscribe(\'comment.create\', function () {
FB.api(\'/comments/?ids=http://foo.com\', function (response) {
console.log(response);
});
});
};
(function () {
var e = document.createElement(\'script\'); e.async = true;
e.src = document.location.protocol + \'//connect.facebook.net/en_US/all.js\';
document.getElementById(\'fb-root\').appendChild(e);
} ());
</script>
查看firebug中的控制台日志,ѭ1显示此对象:
{
\"http://foo.com\": {
\"data\": [
{
\"id\": \"10150090820621770_15631060\",
\"from\": {
\"name\": \"Test User\",
\"id\": \"1234455\"
},
\"message\": \"testing\",
\"created_time\": \"2011-04-18T01:55:38+0000\"
},
{
\"id\": \"10150090820621770_15631066\",
\"from\": {
\"name\": \"Test UserToo\",
\"id\": \"1149043581\"
},
\"message\": \"testing2\",
\"created_time\": \"2011-04-18T01:56:12+0000\"
}
]
}
}
但是,如果尝试使用response.data[0].from.name
访问对象,则会返回undefined
。此外,以下所有内容还返回“ 4”:
response.data
response.data.length
response.data[0]
我不知道如何解析对象以读取属性。有人有提示吗?
没有找到相关结果
已邀请:
3 个回复
妒垮
或
纤洞需匪
那应该给你想要的对象。
土投