如何获得"来源"来自JSON对象

时间:2015-08-15 02:08:31

标签: javascript json facebook facebook-graph-api

我正在尝试在我的网站上构建自定义的Facebook页面Feed。当Facebook页面上的帖子只有图像时效果很好,但是当他们有视频时我们就遇到了问题。我想要发生的是视频的迷你版本,因为它是一个图像本身。但是,我是图表api的新手,因为我不知道如何抓住"来源"有视频的帖子的元素。

以下是JSON objetc的外观:

{
   "name": "Patrick Oliveira",
   "picture": {
      "data": {
         "is_silhouette": false,
         "url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xft1/v/t1.0-1/c13.0.50.50/p50x50/11694800_709940652467661_5760283552162746065_n.jpg?oh=8ce289debf38df76b1a8ad72e9e32950&oe=5641D882&__gda__=1447414822_01484acd6dcdb29c62a7278a140bb592"
      }
   },
   "posts": {
      "data": [
         {
            "full_picture": "https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-xft1/v/t1.0-9/p417x417/11873451_734632043331855_4522184841179328970_n.jpg?oh=67bd84af22a94bc59a2996ff8fa0a8d8&oe=563F7BE8&__gda__=1447502842_a29144ab596e29d3d92d2239d376adc9",
            "message": "Lan\u00e7ado em fevereiro de 1990, o Photoshop completa 25 anos como o programa de edi\u00e7\u00e3o de imagens mais famoso do mundo. Parab\u00e9ns Adobe!",
            "created_time": "2015-08-14T20:52:08+0000",
            "id": "213578595437205_734632043331855"
         },
         {
            "full_picture": "https://scontent.xx.fbcdn.net/hphotos-xft1/v/t1.0-9/q81/s720x720/11415380_700706213391105_871834721491614634_n.jpg?oh=8672f7ae4931323baaf012e5f57a6079&oe=567F5D35",
            "message": "\"Agora eu sei de que \u00e9 feito aquele ch\u00e3o\".\n-- cliente",
            "created_time": "2015-07-04T20:05:01+0000",
            "id": "213578595437205_700706213391105"
         },
         {
            "full_picture": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xpt1/v/t1.0-9/s720x720/11406889_700700410058352_5592320885219405365_n.jpg?oh=acedee420f8e4944201c714e30f70627&oe=5679534C&__gda__=1451225194_477268223eedc2217b2ae1dcdd345399",
            "message": "Levante que hoje \u00e9 Sexta!",
            "created_time": "2015-07-03T09:27:01+0000",
            "id": "213578595437205_700700410058352"
         },
         {
            "full_picture": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQBxQzu6zRD6fM_d&w=720&h=720&url=http\u00253A\u00252F\u00252Fi.ytimg.com\u00252Fvi\u00252F2K9eMZyD5jc\u00252Fmaxresdefault.jpg&cfs=1",
            "message": "Ferramenta poderosa pra quem trabalha com fotos de arquitetura, o Adaptive Wide Angle pode ser um poderoso aliado!\nhttps://www.youtube.com/watch?v=2K9eMZyD5jc",
            "created_time": "2015-07-02T18:25:01+0000",
            "source": "http://www.youtube.com/v/2K9eMZyD5jc?version=3&autohide=1&autoplay=1",
            "id": "213578595437205_700700186725041"
         },
         {
            "full_picture": "https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xft1/v/t1.0-9/s720x720/541646_700699430058450_5858057325981612930_n.jpg?oh=24a5d3e03cf1f782262e0b72d04831b4&oe=567999FA&__gda__=1451117341_93c352ddfa2804fa4047d84ab94978d9",
            "message": "Come\u00e7ando o m\u00eas de Julho com o p\u00e9...",
            "created_time": "2015-07-01T10:23:00+0000",
            "id": "213578595437205_700699430058450"
         }
      ],
      "paging": {
         "previous": "https://graph.facebook.com/v2.4/213578595437205/posts?limit=5&fields=full_picture,message,created_time,source&since=1439585528&access_token=1641569939420394|NyfBDunYwpFoZrtAoCSfqTV2-uU&__paging_token=enc_AdDQS8eEeKGQ3tvUJJxS1kyUXRwabHz2C1GZA0QZCufpZCHklYUI76WLCrigOB3IT1yxUtsCvx60ZA2haPnZA2Xoc2BOvPOLkM9eZBrbbrV1A87hVgIgZDZD&__previous=1",
         "next": "https://graph.facebook.com/v2.4/213578595437205/posts?limit=5&fields=full_picture,message,created_time,source&access_token=1641569939420394|NyfBDunYwpFoZrtAoCSfqTV2-uU&until=1435746180&__paging_token=enc_AdAVBcOniyc1nbopw3Gr3CkmZB1pUDYBmZBghDmQXLunZBbvcuO1xttx6vpnkZBskAawu1sdcfh6qZCrU3sZA5MzZAkRQMT2VALYxOkCJOQZCm38ebDd2wZDZD"
      }
   },
   "id": "213578595437205"
}

第四篇文章是一个视频帖子,所以我想如果我可以在那里抓取那个视频源,我可以生成一个较小的视频源。

以下是我一直在尝试做的事情:

$.getJSON("https://graph.facebook.com/"+$page_id+"/?fields=name,picture,posts{full_picture,message,created_time}&access_token="+$access_token, function(fb) {
        fb.posts.data[3].source;
    });

然而,我唯一得到的是" undefined"。如果我执行了fb.posts.data[3].messagefb.posts.data[3].created_time或其他任何选项,那么它只会返回" undefined"。我做错了什么?

1 个答案:

答案 0 :(得分:0)

我发现了什么是错的,太愚蠢了。请求URL缺少源元素。因此,它有效:

$.getJSON("https://graph.facebook.com/"+$page_id+"/?fields=name,picture,posts{full_picture,message,created_time,source}&access_token="+$access_token, function(fb) {
        fb.posts.data[3].source;
    });

对不起,我很抱歉。