如何遍历下面给出的json数据我遇到错误

时间:2019-05-21 07:58:13

标签: json ajax

  "childtable_set": [
                    {
                        "season": 2014,
                        "goals": 43,
                        "fk": 1
                    },
                    {
                        "season": 2015,
                        "goals": 26,
                        "fk": 1
                    },
                    {
                        "season": 2016,
                        "goals": 37,
                        "fk": 1
                    },
                    {
                        "season": 2017,
                        "goals": 34,
                        "fk": 1
                    },
                    {
                        "season": 2018,
                        "goals": 34,
                        "fk": 1
                    },
                    {
                        "season": 2014,
                        "goals": 43,
                        "fk": 1
                    },
                    {
                        "season": 2015,
                        "goals": 26,
                        "fk": 1
                    },
                    {
                        "season": 2016,
                        "goals": 37,
                        "fk": 1
                    },
                    {
                        "season": 2017,
                        "goals": 34,
                        "fk": 1
                    },
                    {
                        "season": 2018,
                        "goals": 34,
                        "fk": 1
                    },
                    {
                        "season": 2014,
                        "goals": 43,
                        "fk": 1
                    }
                ],
                "player_name": "Lionel Messi",
                "player_id": 1
            }
        ]



 $.ajax({
             url : final,
             dataType: "json",
             type: 'GET',
             success : function (data) {
                  // $("#resp").append("<p>"+(data.player_name)+"</p>")
                  // $("#resp").append("<p>"+data.childtable_set.player_id+"</p>")
                  // $("#resp").append("<p>"+data+"</p>")
                  for(var i = 0; i < data.childtable_set.length; i++){
                    $("#resp").append("<p>"+data.childtable_set[i].goals+"</p>")
                    $("#resp").append("<p>"+data[i].season+"</p>")
                    $("#resp").append("<p>"+data[i].player_id+"</p>")
                  }
                    }
                 });
             });

错误:

TypeError: data[1] is undefined cant access any data pasted complete ajax call please check out and help

1 个答案:

答案 0 :(得分:0)

如果data是您显示给我们的对象,则它不是数组。使用data.player_name访问Player_name。包含目标和内容的数组在data.childtable_set

将代码更改为此:

success : function (data) {
    $("#resp").append("<p>"+(data.player_name)+"</p>")
    // $("#resp").append("<p>"+data+"</p>")
    for(var i = 0; i < data.childtable_set.length; i++){
    $("#resp").append("<p>"+data.childtable_set[i].goals+"</p>")
    $("#resp").append("<p>"+data.childtable_set[i].season+"</p>")
    $("#resp").append("<p>"+data.childtable_set[i].player_id+"</p>")
}