Json,在另一个级别获取信息

时间:2016-11-07 01:10:06

标签: javascript php json getjson

我有json代码,如:

{
    "player": [{
        "player_id": "1",
        "player_name": "Maxfly",
        "player_image": "res_573fc05f57c0e.png",
        "player_background_image": "images/player_backgrounds/581046687fd89.jpg",
        "player_info": "",
        "player_region": "North America",
        "player_teams": [{
            "id": "1",
            "team_name": "Test Team",
            "team_link": "test-team"
        }, {
            "id": "65",
            "team_name": "Test Team 2",
            "team_link": "test-team-2"
        }]
    }]
}

我已经成功获得了player_id和player_name等等。我的问题是我如何才能获得球队?我尝试了以下内容:

$.getJSON("jsonlink", 
    function(data) {
        $.each(data.player.player_teams, function(i,player_team){

            var append_data = "<div class='item team_item'><div class='row'><div class='col col_img'><a href='/t/" + player_team.team_name + "' ></a></div></div></div>";

            $("#popin-container").append($('<div>' + append_data + '</div>').hide().fadeIn(800));
        });
    });

试图找出我做错了什么。我的json对象是否正确?

谢谢!

1 个答案:

答案 0 :(得分:3)

您的data.player.player_teams错误,因为data.player是一个数组,而不是一个对象。您需要遍历它,或者以简单的方式,您需要附加[0],如下所示:

$.each(data.player[0].player_teams, function(i, player_team) {