循环访问JSON时无法读取未定义的属性0。

时间:2014-02-24 14:33:32

标签: javascript jquery json

我正在使用jQuery ajax方法在JSON中检索一些信息,并收到以下错误。

  

未捕获的TypeError:无法读取未定义的属性“0”

fullName正在成功输出一次。导致此错误的原因是什么?

JSON

{
   "sports":[
      {
         "name":"soccer",
         "id":600,
         "uid":"s:600",
         "leagues":[
            {
               "name":"Barclays Premier League",
               "abbreviation":"eng.1",
               "id":700,
               "isTournament":false,
               "country":{
                  "id":31,
                  "name":"England",
                  "abbreviation":"ENG"
               },
               "uid":"s:600~l:700",
               "groupId":23,
               "shortName":"Premier League",
               "athletes":[
                  {
                     "id":108350,
                     "firstName":"Charlie",
                     "lastName":"Adam",
                     "fullName":"Charlie Adam",
                     "displayName":"Charlie Adam",
                     "shortName":"C. Adam",
                     "links":{
                        "api":{
                           "athletes":{
                              "href":"http://api.espn.com/v1/sports/soccer/eng.1/athletes/108350"
                           },
                           "news":{
                              "href":"http://api.espn.com/v1/sports/soccer/eng.1/athletes/108350/news"
                           },
                           "notes":{
                              "href":"http://api.espn.com/v1/sports/soccer/eng.1/athletes/108350/news/notes"
                           }
                        },
                        "web":{
                           "athletes":{
                              "href":"http://espnfc.com/player/_/id/108350?ex_cid=espnapi_public"
                           }
                        },
                        "mobile":{
                           "athletes":{
                              "href":"http://m.espn.go.com/soccer/profile?id=108350&ex_cid=espnapi_public"
                           }
                        }
                     }
                  },
                  {
                     "id":16684,
                     "firstName":"Emmanuel",
                     "lastName":"Adebayor",
                     "fullName":"Emmanuel Adebayor",
                     "displayName":"Emmanuel Adebayor",
                     "shortName":"E. Adebayor",
                     "links":{
                        "api":{
                           "athletes":{
                              "href":"http://api.espn.com/v1/sports/soccer/eng.1/athletes/16684"
                           },
                           "news":{
                              "href":"http://api.espn.com/v1/sports/soccer/eng.1/athletes/16684/news"
                           },
                           "notes":{
                              "href":"http://api.espn.com/v1/sports/soccer/eng.1/athletes/16684/news/notes"
                           }
                        },
                        "web":{
                           "athletes":{
                              "href":"http://espnfc.com/player/_/id/16684?ex_cid=espnapi_public"
                           }
                        },
                        "mobile":{
                           "athletes":{
                              "href":"http://m.espn.go.com/soccer/profile?id=16684&ex_cid=espnapi_public"
                           }
                        }
                     }
                  },

的jQuery

$.ajax({
            url: "http://api.espn.com/v1/sports/soccer/eng.1/athletes",
            data: {
                // enter your developer api key here
                apikey: "hidden",
                _accept: "application/json"
            },
            dataType: "jsonp",
            success: function(data) {

                console.log(data);

                for (var i = 0; i < data.sports[0].leagues[0].athletes.length; i++) {

                    var data = data.sports[0].leagues[0].athletes[i].fullName;
                    $('body').append('<ul/>');
                    $('body').append('<li>' + data + '</li>');
                } 

            },
            error: function() {
                 console.log('error');
            }
        });

1 个答案:

答案 0 :(得分:3)

你有两个问题。首先是你要覆盖变量数据,因此是未定义的错误。第二个问题是你附加了你的ul和li错误。

success: function(data) {        
    var ul = $('<ul/>').appendTo("body");  //make a ul and append it to the body
    var athletes = data.sports[0].leagues[0].athletes;  //store a reference so code is faster and easier to read
    for (var i = 0; i < athletes.length; i++) {
        var name = athletes[i].fullName;
        ul.append('<li>' + name + '</li>');  //add the li to the new ul you created.
    } 
},