javascript - 如何使用 FB FQL 多查询的结果?

标签 javascript json facebook fql.multiquery

我对 Facebook 的 fql.multiquery 方法感到困惑。

我正在尝试检索一篇帖子的所有评论,然后是每个评论的用户信息。我可以毫无问题地获得评论,但我很难获得用户。

目前我正在使用以下内容:

  FB.api({
   method: 'fql.multiquery',
   queries: {
    query1: 'SELECT post_fbid, fromid, text, time FROM comment WHERE post_id="'+postID +'"',
    query2: 'SELECT id, name, url, pic FROM profile WHERE id IN (SELECT fromid FROM #query1)'
   }
  },
  function(response) {
  }
})

这给了我以下响应:

[
  {
    "name": "query1",
    "fql_result_set": [
      {
        "post_fbid": "xxxxx",
        "fromid": user1id,
        "text": "Here's a comment",
        "time": 1308579931
      },
      {
        "post_fbid": "xxxxx",
        "fromid": user2id,
        "text": "Another comment",
        "time": 1308580031
      }
    ]
  },
  {
    "name": "query2",
    "fql_result_set": [
      {
        "id": user1id,
        "name": "User 1 name",
        "url": "User 1 url",
        "pic": "User 1 pic"
      },
      {
        "id": user2id,
        "name": "User 2 name",
        "url": "User 2 url",
        "pic": "User 2 pic"
      }
    ]
  }
]

问题是,我不知道如何匹配这些!所以我循环浏览评论,并想打印每条评论的文本,旁边有用户名。我该怎么做?

或者,有更好的方法吗?

最佳答案

您可以通过遍历评论并将 fromid 与用户响应中的 id 匹配来匹配这些结果。

例如:

    var comments = response[0].fql_result_set;
    var users = response[1].fql_result_set;    

    //loop through the comments
    for(var i = 0, j = comments.length; i<j; i++){

        for(var x = 0, y = users.length; x<y; x++){
             if(comments[i].fromid == users[x].id){
                 //we have a match, this comment is from user users[x]
                 //process users[x]
                 //break the inner for loop, since we already have a match
             }
        }

    }

关于javascript - 如何使用 FB FQL 多查询的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6459292/

相关文章:

javascript - 带 React 的动态 JQuery 菜单

android - 使用 Facebook 等从 Android 客户端登录到 Openfire 服务器

iOS Facebook 登录不起作用

c++ - RapidJSON : Differences between building using Document or Writer

javascript - 在 Three.js 中的下一帧之前清除屏幕?

javascript - d3 用一个函数设置多个属性

javascript - 如果名称作为变量给出,如何访问多维数组的成员值

java - 从 yaml Autowiring springboot 中的 json 值

javascript - 带有自定义参数的 Facebook 自定义共享 url...在预览中有效但在 FB 上损坏

javascript - 如何从数组而不是字符串中控制台记录数字