cod如何解析HuffingtonPost Pollster API的大型JSON文件

时间:2016-08-20 02:03:39

标签: javascript json parsing

我正在尝试访问:

选择:“克林顿”, 值:“42.6”,

选择:“特朗普”, 价值:“37.7”

来自以下API端点的

http://elections.huffingtonpost.com/pollster/api/charts.json?topic=2016-president&state=us

我正在尝试从第一个JSON对象获取值,但它返回多个值。我看到的大多数JSON教程和问题都是标准的JSON,我可以抓住它的价值,但这个是骗我的,我无法弄清楚

我认为pollObject [0] .estimates [0] .value和pollObject [0] .estimates [1] .value

会工作但不是......我如何在javascript中获取这些值?我正在尝试将它们用于学校项目

我的代码:

'use strict';
var _ = require('lodash');
var requestPromise = require('request-promise');
var ENDPOINT = 'http://elections.huffingtonpost.com/pollster/api/charts.json?topic=2016-president&state=us';

function pollDataHelper() {

}

pollDataHelper.prototype.getPoll = function() {
    var options = {
        method: 'GET',
        uri: ENDPOINT,
        json: true
    };
    console.log('hello333');
    return requestPromise(options);
}

pollDataHelper.prototype.formatePollData = function(pollObject) {
    var template = _.template("Currently Clinton is at ${clintonPoll} percent")
    + "Trump is at ${trumpPoll} percent";
    return template({
        clintonPoll: pollObject[0].estimates[0].value,
        trumpPoll: pollObject[0].estimates[1].value
    });
}

module.exports = pollDataHelper;

2 个答案:

答案 0 :(得分:0)

尝试代码

var data = JSON.parse('[{"id":797,"title":"2016 General Election: Trump vs. Clinton vs. Johnson","slug":"2016-general-election-trump-vs-clinton-vs-johnson","topic":"2016-president","state":"US","short_title":"2016 President: Trump vs. Clinton vs. Johnson","election_date":"2016-11-08","poll_count":285,"last_updated":"2016-08-20T00:42:15.000Z","url":"http://elections.huffingtonpost.com/pollster/2016-general-election-trump-vs-clinton-vs-johnson","estimates":[{"choice":"Clinton","value":"42.6","lead_confidence":100.0,"first_name":"Hillary","last_name":"Clinton","party":"Dem","incumbent":false},{"choice":"Trump","value":"35.7","lead_confidence":0.0,"first_name":"Donald","last_name":"Trump","party":"Rep","incumbent":false},{"choice":"Johnson","value":"8.7","lead_confidence":null,"first_name":"Gary","last_name":"Johnson","party":"Lib","incumbent":false},{"choice":"Other","value":"4.0","lead_confidence":null,"first_name":"","last_name":"Other","party":null,"incumbent":false}]},{"id":670,"title":"2016 General Election: Santorum vs. Clinton","slug":"2016-general-election-santorum-vs-clinton","topic":"2016-president","state":"US","short_title":"2016 President: Santorum vs. Clinton","election_date":"2016-11-08","poll_count":4,"last_updated":"2016-06-17T21:23:44.000Z","url":"http://elections.huffingtonpost.com/pollster/2016-general-election-santorum-vs-clinton","estimates":[]},{"id":640,"title":"2016 General Election: Trump vs. Sanders","slug":"2016-general-election-trump-vs-sanders","topic":"2016-president","state":"US","short_title":"2016 President: Trump vs. Sanders","election_date":"2016-11-08","poll_count":93,"last_updated":"2016-08-20T00:37:41.000Z","url":"http://elections.huffingtonpost.com/pollster/2016-general-election-trump-vs-sanders","estimates":[{"choice":"Sanders","value":"49.9","lead_confidence":100.0,"first_name":"Bernie","last_name":"Sanders","party":"Dem","incumbent":false},{"choice":"Trump","value":"38.9","lead_confidence":0.0,"first_name":"Donald","last_name":"Trump","party":"Rep","incumbent":false},{"choice":"Other","value":"3.9","lead_confidence":null,"first_name":null,"last_name":null,"party":"N/A","incumbent":false}]},{"id":624,"title":"2016 General Election: Trump vs. Clinton","slug":"2016-general-election-trump-vs-clinton","topic":"2016-president","state":"US","short_title":"2016 President: Trump vs. Clinton","election_date":"2016-11-08","poll_count":285,"last_updated":"2016-08-20T01:39:11.000Z","url":"http://elections.huffingtonpost.com/pollster/2016-general-election-trump-vs-clinton","estimates":[{"choice":"Clinton","value":"47.6","lead_confidence":100.0,"first_name":"Hillary","last_name":"Clinton","party":"Dem","incumbent":false},{"choice":"Trump","value":"39.5","lead_confidence":0.0,"first_name":"Donald","last_name":"Trump","party":"Rep","incumbent":false},{"choice":"Other","value":5.8,"lead_confidence":null,"first_name":"","last_name":"Other","party":null,"incumbent":false}]}]');
console.log(data[0].estimates[0].value);
console.log(data[0].estimates[1].value);

如果您想获得所有估算数据。你应该使用循环

答案 1 :(得分:0)

试试我的代码。它运行成功。

var request = require("request");
request({
      uri: "http://elections.huffingtonpost.com/pollster/api/charts.json?topic=2016-president&state=us",

}, function(error, response, body) {
     // console.log(response.body[0].estimates[0].value); ==> undefined
    var results = JSON.parse(response.body);
    console.log(results[0].estimates[0].value);  // ==> value = 42.6
    console.log(results[0].estimates[1].value);  // ==> value = 35.7
});