How to read attribute data from an object returned from FB.api?

I have the following code to catch an event when a user leaves a comment. This works correctly, but the problem is that I have no idea how to parse the object that is 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 firebug console log 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 using response.data[0].from.name, I get undefined. In addition, all of the following returns are undefinedalso:

  • response.data
  • response.data.length
  • response.data[0]

I do not know how to parse an object to read attributes. Does anyone have any clues?

+3
source share
3 answers

"http://foo.com".. - response["http://foo.com"].data[0].id response["http://foo.com"].data[0].from.name

+2

, . , javascript-. JSON, . , JSON ( wikipedia : ff 3.5+, IE8 +, opera 10.5+, webkit), :

var responseObject = JSON.parse(response);

, .

0

I used FQL to access FB comments.

0
source

All Articles