Facebook Graph API - 我/喜欢返回的数组的名称是什么

Facebook Graph API - What's the name of the array me/likes returns?

本文关键字:数组 是什么 返回 喜欢 API Graph Facebook      更新时间:2023-09-26

我使用 de Javascript SDK 连接到 Facebook Graph API 并检索我自己的喜欢。我的代码在控制台中返回一个包含多个对象的 JSON 数组。它返回的数组的名称是什么?

我需要知道这一点,这样我才能定位每个对象的特定字段,并通过JS和HTML将它们显示在网页上。

我的图形 API 调用:

function getLikes() {
        FB.api(
            "/me/likes",
            function (response) {
                if (response && !response.error) {
                    console.log(response)
                }
            }
);

控制台中的 JSON 响应:

https://i.stack.imgur.com/C8Rb7.png

简单的一个:

GET /me?fields=id,name,likes

输出类似的东西

{
  "id": "372934792749234729479", 
  "name": "Firstname Lastname", 
  "likes": {
    "data": [
      {
        "name": "Golf", 
        "category": "Sport", 
        "id": "105942022769573", 
        "created_time": "2015-01-01T14:45:39+0000"
      }
    ], 
    "paging": {
      "cursors": {
        "before": "Njg0MTM5ODQyMjk=", 
        "after": "MTA1OTQyMDIyNzY5NTcz"
      }
    }
  }
}

所以likes.data是你想要的东西。

下面是一个关于如何显示喜欢的示例:

function getLikes() {
    FB.api("/me/likes", function (response) {
        if (response && !response.error) {
            console.log(response);
            response.data.forEach(function(like, index, array) {
                console.log("Like " + (index+1) + ": " + like.name + " with id " + like.id);
            });
        }
    });
}