How to read attribute data from the object returned from FB.api?
I have the following code implement to catch the event when a user leaves a comment. It is firing correctly, but the problem is I have no idea how to parse the object that is being passed to my callback function.
<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>
Looking at the console log in firebug, console.log(response)
shows this object:
{
"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"
}
]
}
}
However, if I try to access the object with response.data[0].from.name
I get undefined
returned. Additionally, all the following return 开发者_如何学JAVAundefined
also:
response.data
response.data.length
response.data[0]
I have no idea how to parse the object to read the attributes. Anyone have any tips?
You forget your "http://foo.com" ..
So it should be something like response["http://foo.com"].data[0].id
Or response["http://foo.com"].data[0].from.name
I think the response is text. You need to get the compiler to parse the text into a javascript object. since the text is formatted as JSON, that's pretty easy. on browsers that support JSON (the wikipedia page says: ff 3.5+, IE8+, opera 10.5+, webkit based stuff), you can do the following:
var responseObject = JSON.parse(response);
that should get you the object you want.
I have used the FQL to access FB comments.
精彩评论