我有json数据,如:
{
"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"
}
}
}
我的代码是:
$('#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');
}
});
错误是:
Uncaught TypeError: Cannot read property 'statistics' of undefined
$.ajax.success
c
p.fireWith
k
r
我在这里做错了什么?
我是获取json数据的新手,所以请帮助我!谢谢你们! :)
答案 0 :(得分:2)
你是否在将它用作json对象之前检查是否应首先解析它? 像这样;
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>');
});
答案 1 :(得分:1)
您没有属性id
,您必须使用6217895
,但这不是有效的属性名称,因此json似乎无效。
正确的路径是stats.data.6217895.statistics.all
,但正如我之前所说,这是无效的,会引发错误。
答案 2 :(得分:1)
您的id
可能是dynamic
,请尝试此操作,
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>');
});
});
答案 3 :(得分:1)
如果单个json中有多个id,则@Rohan Kumar代码将起作用。如果你总是得到相同的json,但有不同的id,那么这对你有用:)
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>');
});