christopher.web christopher.web - 27 days ago 6
Javascript Question

Json, get info within another level

I have json code that comes out like:

{
"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"
}]
}]
}


I've managed to get the player_id and player_name etc. My question is how to I just get the teams? I've tried the following:

$.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));
});
});


Trying to figure out what I'm doing wrong. Is my json object correct?

Thanks!

Answer

Your data.player.player_teams is wrong, as data.player is an array, and not an object. You need to loop through it, or in simple way, you need to attach a [0] like this:

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