2

我正在尝试使用 espn 公共 API,并尝试使用他们的 json 访问 NFL 球员信息。

json 即时访问成功看起来像:

{
"sports": [
    {
        "name": "football",
        "id": 20,
        "leagues": [
            {
                "name": "National Football League",
                "abbreviation": "nfl",
                "id": 28,
                "groupId": 9,
                "shortName": "NFL",
                "athletes": [
                    {
                        "id": 14466,
                        "firstName": "Isa",
                        "lastName": "Abdul-Quddus",
                        "fullName": "Isa Abdul-Quddus",
                        "displayName": "Isa Abdul-Quddus",
                        "shortName": "I. Abdul-Quddus",
                        "links": {
                            "api": {
                                "athletes": {
                                    "href": "http://api.espn.com/v1/sports/football/nfl/athletes/14466"
                                },
                                "news": {
                                    "href": "http://api.espn.com/v1/sports/football/nfl/athletes/14466/news"
                                },
                                "notes": {
                                    "href": "http://api.espn.com/v1/sports/football/nfl/athletes/14466/news/notes"
                                }
                            },
                            "web": {
                                "athletes": {
                                    "href": "http://espn.go.com/nfl/player/_/id/14466/isa-abdul-quddus?ex_cid=espnapi_public"
                                }
                            },
                            "mobile": {
                                "athletes": {
                                    "href": "http://m.espn.go.com/nfl/playercard?playerId=14466&ex_cid=espnapi_public"
                                }
                            }
                        }
                    },
                    {
                        "id": 8645,
                        "firstName": "Hamza",
                        "lastName": "Abdullah",
                        "fullName": "Hamza Abdullah",
                        "displayName": "Hamza Abdullah",
                        "shortName": "H. Abdullah",
                        "links": {
                            "api": {
                                "athletes": {
                                    "href": "http://api.espn.com/v1/sports/football/nfl/athletes/8645"
                                },
                                "news": {
                                    "href": "http://api.espn.com/v1/sports/football/nfl/athletes/8645/news"
                                },
                                "notes": {
                                    "href": "http://api.espn.com/v1/sports/football/nfl/athletes/8645/news/notes"
                                }
                            },
                            "web": {
                                "athletes": {
                                    "href": "http://espn.go.com/nfl/player/_/id/8645/hamza-abdullah?ex_cid=espnapi_public"
                                }
                            },
                            "mobile": {
                                "athletes": {
                                    "href": "http://m.espn.go.com/nfl/playercard?playerId=8645&ex_cid=espnapi_public"
                                }
                            }
                        }
                    },
                    {
                        "id": 11910,
                        "firstName": "Husain",
                        "lastName": "Abdullah",
                        "fullName": "Husain Abdullah",
                        "displayName": "Husain Abdullah",
                        "shortName": "H. Abdullah",
                        "links": {
                            "api": {
                                "athletes": {
                                    "href": "http://api.espn.com/v1/sports/football/nfl/athletes/11910"
                                },
                                "news": {
                                    "href": "http://api.espn.com/v1/sports/football/nfl/athletes/11910/news"
                                },
                                "notes": {
                                    "href": "http://api.espn.com/v1/sports/football/nfl/athletes/11910/news/notes"
                                }
                            } ........
                ]
            }
        ]
    }
],
"resultsOffset": 0,
"resultsLimit": 50,
"resultsCount": 3301,
"timestamp": "2013-01-06T19:30:17Z",
"status": "success"

}

继承人的 html / javascript 我正在使用:

$(document).ready(function(){

    $.getJSON("http://api.espn.com/v1/sports/football/nfl/athletes?apikey=MY-API-KEY-HERE&_accept=application/json",
    function(data){
      $.each(data["sports"], function(i,item){
         $("#infoDiv").append( [i] + " - " + item.name + "<br>" );    
      });
    });

});

我可以让它显示 0 - 足球,但不能使用类似的东西

$.each(data["sports"]["leagues"]["athletes"], function(i,item){
  $("#infoDiv").append( [i] + " - " + item.firstName + "<br>" );  

访问个人运动员数据,例如 item.firstName 等。

我不断收到以下错误:

TypeError: data.sports.leagues is undefined

我错过了什么?我成功地将相同的代码结构与其他几个提供 json 的 API 一起使用。相比之下,ESPN json 稍微复杂一些。

感谢您为我提供的任何信息。

4

2 回答 2

3

运动、联赛和运动员是数组,例如:sports[0] 是一个对象(名称='football'的对象)你应该像这样迭代每个对象(未测试):

$.each(data.sports, function(i,sport) {
    $.each(sport.leagues, function(i,league) {
        $.each(league.athletes, function(i,athlete) {
            $("#infoDiv").append( [i] + " - " + athlete.firstName + "<br>" );
        });
    });
});
于 2013-01-07T13:57:31.740 回答
1

sports,leagues并且athletes是您需要迭代的数组。

for (var i=0; i<data.sports.length; i++) {
     var sport = data.sports[i];
     $("#infoDiv").append( [i] + " - " + sport.name + "<br>" );
     for (var j=0; j<sport.leagues.length; j++) {
          var league = sport.leagues[j];
          $("#infoDiv").append( [i,j] + " - " + league.name + "<br>" );
          for (var k=0; k<league.athletes.length; k++) {
               var athlete = league.athletes[k];
               $("#infoDiv").append( [i,j,k] + " - " + athlete.fullName + "<br>" );
          }
     }
}
于 2013-01-07T14:22:52.267 回答