2013-01-07 73 views
1

我正在試驗espn公共API,並試圖使用他們的json來訪問NFL播放器信息。深入瞭解json數據

的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比較複雜一些。

感謝您爲我提供的任何光線。

回答

3

運動,聯賽和運動員是數組,例如:體育[0]是一個對象(具有名稱=「足球」) 你應該遍歷每個這樣的(未測試):

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

完美地工作。謝謝巴勃羅。所以他們'嵌套'(如果這是正確的術語),我需要使用.each逐步通過'每個'級別。 – tamak

+0

@tamak不客氣。在每個運動對象的層次上,_name_和_leagues_都嵌套**,不同之處在於數組在同一嵌套層次上具有多個對象,因此您必須對它們進行迭代。如果答案滿足您,請將其標記爲已接受。 –

1

sports,leaguesathletes是您需要迭代的數組。

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