Reputation: 841
i have the json data like:
{
"status": "ok",
"count": 1,
"data": {
"6217895": {
"clan": {
"role_i18n": "Saha Komutanı",
"clan_id": 16682,
"role": "commander",
"since": 1361201722
},
"achievements": {
"tank_expert_uk": 0,
"medal_dumitru": 3,
"invader": 10,
"medal_lehvaslaiho": 0,
"warrior": 60,
"medal_halonen": 1,
"medal_pascucci": 6,
"medal_orlik": 0,
"medal_brothers_in_arms": 30,
"mousebane": 0,
"tank_expert_france": 0,
"mechanic_engineer_ussr": 0,
"medal_bruno_pietro": 0,
"medal_delanglade": 1,
"lucky_devil": 3,
"defender": 39,
"armor_piercer": 1,
"medal_kay": 2,
"supporter": 115,
"mechanic_engineer": 0,
"steelwall": 185,
"max_sniper_series": 48,
"medal_knispel": 1,
"medal_boelter": 0,
"medal_ekins": 1,
"medal_heroes_of_rassenay": 0,
"medal_tamada_yoshio": 0,
"tank_expert_usa": 0,
"mechanic_engineer_germany": 0,
"max_piercing_series": 26,
"tank_expert": 0,
"iron_man": 4,
"medal_radley_walters": 5,
"kamikaze": 6,
"tank_expert_germany": 0,
"beasthunter": 8,
"sniper": 429,
"medal_tarczay": 0,
"medal_lavrinenko": 2,
"mechanic_engineer_france": 0,
"medal_oskin": 0,
"medal_burda": 0,
"medal_billotte": 0,
"huntsman": 0,
"hand_of_death": 1,
"medal_fadin": 0,
"medal_lafayette_pool": 0,
"max_killing_series": 7,
"tank_expert_china": 0,
"mechanic_engineer_usa": 0,
"medal_kolobanov": 0,
"patton_valley": 0,
"bombardier": 1,
"medal_abrams": 2,
"max_invincible_series": 3,
"medal_poppel": 2,
"medal_crucial_contribution": 0,
"raider": 1,
"max_diehard_series": 9,
"mechanic_engineer_uk": 0,
"invincible": 0,
"lumberjack": 0,
"sturdy": 57,
"title_sniper": 1,
"sinai": 13,
"diehard": 0,
"medal_carius": 1,
"medal_le_clerc": 2,
"tank_expert_ussr": 0,
"evileye": 11,
"mechanic_engineer_china": 0,
"medal_nikolas": 0,
"scout": 24
},
"statistics": {
"clan": {
"spotted": 0,
"hits": 13,
"battle_avg_xp": 716,
"draws": 1,
"wins": 1,
"losses": 0,
"capture_points": 0,
"battles": 2,
"damage_dealt": 2773,
"hits_percents": 76,
"damage_received": 2889,
"shots": 17,
"xp": 1432,
"frags": 1,
"survived_battles": 1,
"dropped_capture_points": 0
},
"all": {
"spotted": 13298,
"hits": 63481,
"battle_avg_xp": 572,
"draws": 124,
"wins": 5603,
"losses": 4587,
"capture_points": 15274,
"battles": 10314,
"damage_dealt": 11681437,
"hits_percents": 72,
"damage_received": 9508670,
"shots": 88151,
"xp": 5899761,
"frags": 10139,
"survived_battles": 2916,
"dropped_capture_points": 10477
},
"company": {
"spotted": 117,
"hits": 1565,
"battle_avg_xp": 722,
"draws": 2,
"wins": 166,
"losses": 87,
"capture_points": 588,
"battles": 255,
"damage_dealt": 347292,
"hits_percents": 79,
"damage_received": 250625,
"shots": 1977,
"xp": 184204,
"frags": 231,
"survived_battles": 125,
"dropped_capture_points": 436
},
"max_xp": 2566
},
"account_id": 6217895,
"created_at": 1333888616,
"updated_at": 1382372956,
"private": null,
"nickname": "lsvenom"
}
}
}
and my code is:
$('#form1').submit(function(){
var nick=$('#username').val();
if(nick.length > 0){
var url='http://api.worldoftanks.ru/2.0/account/list/?application_id=171745d21f7f98fd8878771da1000a31&search='+nick;
$.getJSON(url, function(json) {
$('#result').html('');
$.each(json.data, function(index, data) {
if(index=='0'){
$('#result').append('<p> name: ' + data.nickname+ '</p>');
$('#result').append('<p> ID: ' +data.id+ '</p>');
var id=data.id;
var url2='http://api.worldoftanks.ru/2.0/account/info/?application_id=171745d21f7f98fd8878771da1000a31&account_id='+id;
$.ajax({
type: "GET",
url: url2,
dataType: 'json',
crossDomain: true,
success: function(stats) {
stats = JSON.parse(stats);
$.each(stats.data.id.statistics.all, function(i, all) {
var wins=all.wins;
var losses=all.losses;
$('#result').append('<p>Wins: ' +wins+ '</p>');
$('#result').append('<p>Losses: ' +losses+ '</p>');
});
}
});
}
});
});
}else{
alert('enter username');
}
});
the mistake is:
Uncaught TypeError: Cannot read property 'statistics' of undefined
$.ajax.success
c
p.fireWith
k
r
What im doing wrong here?
im newbie in getting json data, so please help me! Thank you all! :)
Upvotes: 0
Views: 307
Reputation: 2770
if there are multiple id's in single json then @Rohan Kumar code will work. If you will always get same json but with different id then this would work for you :)
stats = JSON.parse(stats);
$.each(stats.data[0].statistics.all, function(i, all) {
var wins=all.wins;
var losses=all.losses;
$('#result').append('<p>Wins: ' +wins+ '</p>');
$('#result').append('<p>Losses: ' +losses+ '</p>');
});
Upvotes: 1
Reputation: 40639
Your id
may be dynamic
so try this,
stats = JSON.parse(stats);
$.each(stats.data, function(i, id){
$(id.statistics.all).each(function(j,all){
var wins=all.wins;
var losses=all.losses;
$('#result').append('<p>Wins: ' +wins+ '</p>');
$('#result').append('<p>Losses: ' +losses+ '</p>');
});
});
Upvotes: 1
Reputation: 5207
did you check whether you should first parse it before using it as a json object? like this;
stats = JSON.parse(stats);
$.each(stats.data.eval(id).statistics.all, function(i, all) {
var wins=all.wins;
var losses=all.losses;
$('#result').append('<p>Wins: ' +wins+ '</p>');
$('#result').append('<p>Losses: ' +losses+ '</p>');
});
Upvotes: 2
Reputation: 68440
You have no property id
, you have to use 6217895
but that's not a valid property name so that json seems to be invalid.
The correct path would be stats.data.6217895.statistics.all
but, as I said before, this is invalid and would throw an error.
Upvotes: 1