English 中文(简体)
如何从FB.api返回的对象中读取属性数据?
原标题:How to read attribute data from the object returned from 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中的控制台日志,console.log(response)显示了以下对象:

{
    "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"
          }
       ]
    }
 }

但是,如果我尝试访问带有<code>response.data[0].from.name</code>的对象,我会返回<code>undefined</code〕。此外,以下所有返回的<code>undefined</code>也是:

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

我不知道如何解析对象来读取属性。有人有什么建议吗?

最佳回答

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

问题回答

我认为回复是文本。您需要让编译器将文本解析为javascript对象。由于文本被格式化为JSON,所以这很容易。在支持JSON的浏览器上(维基百科页面上写着:ff 3.5+、IE8+、opera 10.5+、基于webkit的东西),您可以执行以下操作:

var responseObject = JSON.parse(response);

那应该能给你想要的东西。

我使用了FQL访问FB评论。





相关问题
Facebook Connect login dialog not working

I am using Facebook Connect for iPhone and following the official instructions. I use the following code to display the login dialog: FBLoginDialog* dialog = [[[FBLoginDialog alloc] initWithSession:...

Facebook App Profile Tab is Empty ... No Content Displayed?

I can view my application via the http://apps.facebook.com/myapplication/ link and the content shows up correctly. I also added the application as a tab to a facebook page. However, when viewing the ...

Facebook Platform error: "Object cannot be liked"

I m working on a Facebook Application that generates wall posts. In testing these posts, I ve discovered that the Facebook Platform action of "liking" a post is failing. The specific error message ...

how to call showPermissionsDialog() in php (facebook api)?

I was reading over the documentation yet I could not figure out how to call Facebook.showPermissionsDialog() in php include_once ./facebook-platform/php/facebook.php ; $facebook = new Facebook(my ...

Facebook connect

If I plug in the facebook connect into my website, How can I prevent double signups? Lets say I have a user that s already signed up to my site but he clicked the connect with facebook, is there a ...

热门标签