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

codHow to parse large JSON file of HuffingtonPost Pollster API

本文关键字:HuffingtonPost Pollster API 文件 何解析 大型 JSON      更新时间:2023-09-26

我正在尝试访问:

选择:"克林顿",价值:"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;

试码

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);

如果你想获得所有的估计数据。你应该使用loop

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

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
});