简体   繁体   English

REST API调用从JSON返回未定义的列表

[英]REST API call returning undefined list from JSON

I'm attempting to display a list of presidential candidate names from the FEC REST API on a website and am getting a list of three bulleted items that just say "undefined." 我正在尝试在网站上显示FEC REST API中的总统候选人名单,并获得三个项目符号列表,这些项目只是说“未定义”。

I've tried this with other APIs like https://ghibliapi.herokuapp.com/films and it works perfectly. 我已经尝试使用其他API,如https://ghibliapi.herokuapp.com/films ,它完美无缺。 Something about the FEC structure of the JSON file is different - I'm not sure if reason for this JSON being treated differently is the pagination or something else. 关于JSON文件的FEC结构的一些东西是不同的 - 我不确定这个JSON被区别对待的原因是分页还是别的。

Here are the first lines of the raw JSON: 以下是原始JSON的第一行:

    {
      "api_version": "1.0",
      "pagination": {
        "pages": 1,
        "per_page": 20,
        "page": 1,
        "count": 20
      },
      "results": [
        {
          "election_years": [
            2008,
            2016
          ],
          "two_year_period": 2020,
          "last_file_date": "2016-07-30",
          "federal_funds_flag": false,
          "name": "SANDERS, BERNARD"
      }
   }


$.getJSON("https://api.open.fec.gov/v1/candidates/totals/?office=P&election_full=false&per_page=20&cycle=2020&min_receipts=100000&sort_null_only=false&sort_nulls_last=false&page=1&sort_hide_null=false&api_key=MYKEYISHERE", 
function(data) {
    console.log(data);
    $.each(data, function(i, item){
        $('ul#candidates').append('<li>'+item.name+'</li>');
    });

}); });

    <body>
            <div class="cand-container">
                    <img class="photo">
                    <ul id="candidates"></p>
                    <p class="receipts"></p>
            </div>
    </body>

Thank you in advance. 先感谢您。

当你应该循环遍历data.results时,你正试图遍历data

You have to iterate over the data.results . 你必须迭代data.results Data does not have name attributes. 数据没有name属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM