在javascript中使用json检索的数据时遇到问题,无法对数组进行排序

Having trouble using data retrieved with json in javascript, cannot sort the array

本文关键字:问题 数组 排序 遇到 javascript json 数据 检索      更新时间:2023-09-26

我正试图用javascript对从api中检索到的数据进行排序。

代码:

function getResults() {
    var url = $.getJSON("http://api.api.com&leagues=SOCENGPRE&lang=en&format=jsonp&callback=?", function(jsondata){;
        for(var i = 0; i < jsondata.length; i++)
            for(var id in jsondata[i]) {
                console.log("ID - " + id);
                console.log("TEAM - " + jsondata[i][id].home_team);
            }
    });
}

检索到的数据示例:

{
    "SOCENGPRE": {
        "league_name": "Barclays Premier League",
        "league_phid": null,
        "league_type": null,
        "fixtures": [
            {
                "id": "64714",
                "code": "SOCENGPRE",
                "event_slug": "west_ham-tottenham-1401290",
                "home_team": "West Ham",
                "away_team": "Tottenham",
            },
            {
                "id": "64711",
                "code": "SOCENGPRE",
                "event_slug": "manchester_u-sunderland-1401286",
                "home_team": "Manchester U",
                "away_team": "Sunderland"
            }

但是使用我的代码似乎无法得到我想要的结果。

我想打印出每一场比赛的ID和主队。关于为什么我的代码不起作用,有什么见解吗?提前非常感谢!

更新:我已经删除了多余的分号,但它仍然没有为我打印数据。更新2:关于URL的请求。当我在浏览器中调用它时,我会得到这个巨大的结果

?({"SOCENGPRE":{"league_name":"Barclays Premier League","league_phid":null,"league_type":null,"fixtures":[{"id":"64714","code":"SOCENGPRE","event_slug":"west_ham-tottenham-1401290","start":"1399117500","home_team":"West Ham","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t523.png","home_team_short":"","away_team":"Tottenham","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t498.png","away_team_short":"","phid":null},{"id":"64711","code":"SOCENGPRE","event_slug":"manchester_u-sunderland-1401286","start":"1399125600","home_team":"Manchester U","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t20790.png","home_team_short":"Man U","away_team":"Sunderland","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t382.png","away_team_short":"","phid":null},{"id":"64712","code":"SOCENGPRE","event_slug":"stoke-fulham-1401288","start":"1399125600","home_team":"Stoke","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t389.png","home_team_short":"","away_team":"Fulham","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t379.png","away_team_short":"","phid":null},{"id":"64706","code":"SOCENGPRE","event_slug":"aston_villa-hull-1401282","start":"1399125600","home_team":"Aston Villa","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t380.png","home_team_short":"","away_team":"Hull","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t511.png","away_team_short":"Hull","phid":null},{"id":"64710","code":"SOCENGPRE","event_slug":"newcastle-cardiff-1401287","start":"1399125600","home_team":"Newcastle","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t385.png","home_team_short":"","away_team":"Cardiff","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t524.png","away_team_short":"","phid":null},{"id":"64713","code":"SOCENGPRE","event_slug":"swansea-southampton-1401289","start":"1399125600","home_team":"Swansea","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t384.png","home_team_short":"Swansea","away_team":"Southampton","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t8482.png","away_team_short":"","phid":null},{"id":"64709","code":"SOCENGPRE","event_slug":"everton-manchester_c-1401285","start":"1399134600","home_team":"Everton","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t499.png","home_team_short":"","away_team":"Manchester C","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t383.png","away_team_short":"Man C","phid":null},{"id":"64707","code":"SOCENGPRE","event_slug":"arsenal-west_bromwich-1401281","start":"1399206600","home_team":"Arsenal","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t30773.png","home_team_short":"","away_team":"West Bromwich","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t816.png","away_team_short":"West Brom","phid":null},{"id":"64705","code":"SOCENGPRE","event_slug":"chelsea-norwich-1401283","start":"1399215600","home_team":"Chelsea","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t497.png","home_team_short":"","away_team":"Norwich","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t1438.png","away_team_short":"","phid":null},{"id":"64708","code":"SOCENGPRE","event_slug":"crystal_palace-liverpool-1401284","start":"1399316400","home_team":"Crystal Palace","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t518.png","home_team_short":"C. Palace","away_team":"Liverpool","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t381.png","away_team_short":"","phid":null},{"id":"64679","code":"SOCENGPRE","event_slug":"manchester_u-hull-1401252","start":"1399401900","home_team":"Manchester U","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t20790.png","home_team_short":"Man U","away_team":"Hull","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t511.png","away_team_short":"Hull","phid":null},{"id":"64630","code":"SOCENGPRE","event_slug":"manchester_c-aston_villa-1401198","start":"1399488300","home_team":"Manchester C","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t383.png","home_team_short":"Man C","away_team":"Aston Villa","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t380.png","away_team_short":"","phid":null},{"id":"64621","code":"SOCENGPRE","event_slug":"sunderland-west_bromwich-1401189","start":"1399488300","home_team":"Sunderland","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t382.png","home_team_short":"","away_team":"West Bromwich","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t816.png","away_team_short":"West Brom","phid":null},{"id":"64719","code":"SOCENGPRE","event_slug":"manchester_c-west_ham-1401296","start":"1399816800","home_team":"Manchester C","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t383.png","home_team_short":"Man C","away_team":"West Ham","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t523.png","away_team_short":"","phid":null},{"id":"64717","code":"SOCENGPRE","event_slug":"liverpool-newcastle-1401295","start":"1399816800","home_team":"Liverpool","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t381.png","home_team_short":"","away_team":"Newcastle","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t385.png","away_team_short":"","phid":null},{"id":"64720","code":"SOCENGPRE","event_slug":"norwich-arsenal-1401297","start":"1399816800","home_team":"Norwich","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t1438.png","home_team_short":"","away_team":"Arsenal","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t30773.png","away_team_short":"","phid":null},{"id":"64715","code":"SOCENGPRE","event_slug":"fulham-crystal_palace-1401293","start":"1399816800","home_team":"Fulham","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t379.png","home_team_short":"","away_team":"Crystal Palace","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t518.png","away_team_short":"C. Palace","phid":null},{"id":"64722","code":"SOCENGPRE","event_slug":"sunderland-swansea-1401299","start":"1399816800","home_team":"Sunderland","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t382.png","home_team_short":"","away_team":"Swansea","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t384.png","away_team_short":"Swansea","phid":null},{"id":"64723","code":"SOCENGPRE","event_slug":"tottenham-aston_villa-1401300","start":"1399816800","home_team":"Tottenham","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t498.png","home_team_short":"","away_team":"Aston Villa","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t380.png","away_team_short":"","phid":null},{"id":"64724","code":"SOCENGPRE","event_slug":"west_bromwich-stoke-1401301","start":"1399816800","home_team":"West Bromwich","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t816.png","home_team_short":"West Brom","away_team":"Stoke","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t389.png","away_team_short":"","phid":null},{"id":"64718","code":"SOCENGPRE","event_slug":"hull-everton-1401294","start":"1399816800","home_team":"Hull","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t511.png","home_team_short":"Hull","away_team":"Everton","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t499.png","away_team_short":"","phid":null},{"id":"64721","code":"SOCENGPRE","event_slug":"southampton-manchester_u-1401298","start":"1399816800","home_team":"Southampton","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t8482.png","home_team_short":"","away_team":"Manchester U","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t20790.png","away_team_short":"Man U","phid":null},{"id":"64716","code":"SOCENGPRE","event_slug":"cardiff-chelsea-1401292","start":"1399816800","home_team":"Cardiff","home_team_phid":null,"home_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t524.png","home_team_short":"","away_team":"Chelsea","away_team_phid":null,"away_team_logo":"'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t497.png","away_team_short":"","phid":null}]}});

链接到pastebin以更容易地读取数据

在解析JSON后更改要调用的getResults函数,如下所示:

var myDataVar;
$.ajax({
    url: "URL to JSON file here",
    dataType: "text", //If you get an error here, change the type to "text"
    success: function (data) {
        myDataVar = $.parseJSON(data);
        getResults();
    }
});

上面的代码将把JSON文件的解析数据保存到一个变量中。

然后,它将调用一个函数来获得结果,即这个函数:

function getResults() {
        var fixturesLength = myDataVar.SOCENGPRE.fixtures.length - 1;
        for (var i = 0; i <= fixturesLength; i++) {
            console.log(myDataVar.SOCENGPRE.fixtures[i].id);
        }
    }

上面的循环将打印到控制台的每一个设备ID。

使用的测试数据:

{
    "SOCENGPRE": {
        "league_name": "Barclays Premier League",
        "league_phid": null,
        "league_type": null,
        "fixtures": [{
            "id": "64714",
            "code": "SOCENGPRE",
            "event_slug": "west_ham-tottenham-1401290",
            "start": "1399117500",
            "home_team": "West Ham",
            "home_team_phid": null,
            "home_team_logo": "'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t523.png",
            "home_team_short": "",
            "away_team": "Tottenham",
            "away_team_phid": null,
            "away_team_logo": "'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t498.png",
            "away_team_short": "",
            "phid": null
        }, {
            "id": "64711",
            "code": "SOCENGPRE",
            "event_slug": "manchester_u-sunderland-1401286",
            "start": "1399125600",
            "home_team": "Manchester U",
            "home_team_phid": null,
            "home_team_logo": "'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t20790.png",
            "home_team_short": "Man U",
            "away_team": "Sunderland",
            "away_team_phid": null,
            "away_team_logo": "'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t382.png",
            "away_team_short": "",
            "phid": null
        }, {
            "id": "64712",
            "code": "SOCENGPRE",
            "event_slug": "stoke-fulham-1401288",
            "start": "1399125600",
            "home_team": "Stoke",
            "home_team_phid": null,
            "home_team_logo": "'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t389.png",
            "home_team_short": "",
            "away_team": "Fulham",
            "away_team_phid": null,
            "away_team_logo": "'/'/dxnxhx88pdxyv.cloudfront.net'/logo'/32'/t379.png",
            "away_team_short": "",
            "phid": null
        }]
    }
}

控制台结果:

64714
64711
64712