如何从node.js中的facebook graph api解析json响应

时间:2016-12-15 20:02:19

标签: javascript json node.js facebook facebook-graph-api

我想解析json来获取一些数据,但我无法解析json如何解析json以从返回的json中获取最新的图片。以下是我的代码。我正在使用facebook graph api来获取nodejs中的数据。

<script>
  $(document).on("submit", false);
</script>

以下是json返回

var params = {
        hostname: 'graph.facebook.com',
        port: 443,
        path: '/' + 'abc' + '/posts?limit=1&access_token=' + access_token,
        method: 'GET'
    };

    https.get(params, function (response) {

        response.setEncoding("utf8");
        var str;

        response.on('data', function (chunk) {
            str += chunk;
        });

        response.on('end', function () {
            let parsedJSON  = JSON.parse(JSON.stringify(str.replace('undefined','')));
            console.log(parsedJSON)
        });

        //response.on("error", callback);
    });

1 个答案:

答案 0 :(得分:0)

这看起来像是一个错误:

    response.on('end', function () {
        let parsedJSON  = JSON.parse(JSON.stringify(str.replace('undefined','')));
        console.log(parsedJSON)
    });

str已经是一个字符串。您不会在字符串上调用JSON.stringify(),或者您最终会对其进行双重编码。我试试

    response.on('end', function () {
        let parsedJSON = JSON.parse(str.replace('undefined',''));
        console.log(parsedJSON)
    });

这是我的完整代码:

'use strict';

const https = require('https');

const access_token = 'EAAaFIbbzLZAkBAKdplMOlhckeDI5W2P37ZB20qMTZCMYfGv2Bw5FQcb7PlCnZBf5b0szAXuKUqTEZCLCSipGZCx4uaF42dAhxcoJ98v7XwEDjT0Y2A2fzlCc7nuUMOc4GCgEZCiQ3yT9sV5lba9GppSqowR8HRg8ZADMJcZAX5Y1qOAZDZD';

const params = {
  hostname: 'graph.facebook.com',
  port: 443,
  path: '/' + 'abc' + '/posts?limit=1&access_token=' + access_token,
  method: 'GET'
};

https.get(params, function(response) {

   response.setEncoding("utf8");
   let str = '';

   response.on('data', chunk => str += chunk);

   response.on('end', () => {
     let parsedJSON  = JSON.parse(str.replace('undefined',''));
     console.log(parsedJSON)
   });
});

这是我的输出:

{ data:
   [ { message: 'Several studies have shown an optimistic thinking style is linked with better health and living longer, and in particular a reduced risk of dying from heart disease. \n\nABC Health & Wellbeing | http://ab.co/2hwLeBx',
       created_time: '2016-12-15T10:00:13+0000',
       id: '194764094549_10154508296244550' } ],
  paging:
   { previous: 'https://graph.facebook.com/v2.8/194764094549/posts?limit=1&since=1481796013&access_token=EAAaFIbbzLZAkBAKdplMOlhckeDI5W2P37ZB20qMTZCMYfGv2Bw5FQcb7PlCnZBf5b0szAXuKUqTEZCLCSipGZCx4uaF42dAhxcoJ98v7XwEDjT0Y2A2fzlCc7nuUMOc4GCgEZCiQ3yT9sV5lba9GppSqowR8HRg8ZADMJcZAX5Y1qOAZDZD&__paging_token=enc_AdApjLiLvzI37u3lbyKcsJFU05GtwQc1RoACciuJw8dxQUWQpqY39UfrjPNpMR9MHMMBTiY4W7EMSFKDf59m19lH6NMlJU0IofZBC7IdRc0gIlAZDZD&__previous=1',
     next: 'https://graph.facebook.com/v2.8/194764094549/posts?limit=1&access_token=EAAaFIbbzLZAkBAKdplMOlhckeDI5W2P37ZB20qMTZCMYfGv2Bw5FQcb7PlCnZBf5b0szAXuKUqTEZCLCSipGZCx4uaF42dAhxcoJ98v7XwEDjT0Y2A2fzlCc7nuUMOc4GCgEZCiQ3yT9sV5lba9GppSqowR8HRg8ZADMJcZAX5Y1qOAZDZD&until=1481796013&__paging_token=enc_AdApjLiLvzI37u3lbyKcsJFU05GtwQc1RoACciuJw8dxQUWQpqY39UfrjPNpMR9MHMMBTiY4W7EMSFKDf59m19lH6NMlJU0IofZBC7IdRc0gIlAZDZD' } }

请注意,此输出是一个对象,由于调用JSON.stringify()额外的时间,您的输出是一个字符串。

以下表格中的JSON对象更容易阅读:

{
    data: [
        {
            message: 'Several studies have shown an optimistic thinking style is linked with better health and living longer, and in particular a reduced risk of dying from heart disease. \n\nABC Health & Wellbeing | http://ab.co/2hwLeBx',
            created_time: '2016-12-15T10:00:13+0000',
            id: '194764094549_10154508296244550'
        }
    ],
    paging: {
        previous: 'https://graph.facebook.com/v2.8/194764094549/posts?limit=1&since=1481796013&access_token=EAAaFIbbzLZAkBAKdplMOlhckeDI5W2P37ZB20qMTZCMYfGv2Bw5FQcb7PlCnZBf5b0szAXuKUqTEZCLCSipGZCx4uaF42dAhxcoJ98v7XwEDjT0Y2A2fzlCc7nuUMOc4GCgEZCiQ3yT9sV5lba9GppSqowR8HRg8ZADMJcZAX5Y1qOAZDZD&__paging_token=enc_AdApjLiLvzI37u3lbyKcsJFU05GtwQc1RoACciuJw8dxQUWQpqY39UfrjPNpMR9MHMMBTiY4W7EMSFKDf59m19lH6NMlJU0IofZBC7IdRc0gIlAZDZD&__previous=1',
        next: 'https://graph.facebook.com/v2.8/194764094549/posts?limit=1&access_token=EAAaFIbbzLZAkBAKdplMOlhckeDI5W2P37ZB20qMTZCMYfGv2Bw5FQcb7PlCnZBf5b0szAXuKUqTEZCLCSipGZCx4uaF42dAhxcoJ98v7XwEDjT0Y2A2fzlCc7nuUMOc4GCgEZCiQ3yT9sV5lba9GppSqowR8HRg8ZADMJcZAX5Y1qOAZDZD&until=1481796013&__paging_token=enc_AdApjLiLvzI37u3lbyKcsJFU05GtwQc1RoACciuJw8dxQUWQpqY39UfrjPNpMR9MHMMBTiY4W7EMSFKDf59m19lH6NMlJU0IofZBC7IdRc0gIlAZDZD'
    }
}

我可以像这样从中提取数据

console.log(parsedJSON.data[0].message);
console.log(parsedJSON.paging.next);