2013-10-23 65 views
0

得到JSON數據我有JSON數據,如:不能從URL

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

什麼即時做錯了這裏?

即時通訊數據的新手,所以請幫助我!謝謝你們! :)

+0

在「$。每個(stats.data.id.statistics.all」,ID沒有被定義? – Johan

+0

' stats.data.statistics.all' – Johan

+0

首先我從暱稱得到id,然後用它來獲取數據 –

回答

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>'); 
     }); 
    }); 
+1

是的這個人正在工作! :) 非常感謝你!但有一些變化,刪除'stats = JSON.parse(stats);' –

1

你沒有財產id,你必須使用6217895但這不是一個有效的屬性名稱,以便json似乎無效。

正確的路徑是stats.data.6217895.statistics.all但是,正如我之前所說,這是無效的,並會引發錯誤。

+0

對不起我的壞,更新了代碼,所以你可以理解我如何使用'ID' –

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

有沒有財產叫'ID' – Johan

+0

@Johan thsat的,因爲沒有屬性稱爲ID,你必須明確地使用'6217895' – pythonian29033

+0

抱歉我的不好,更新了代碼,所以你可以理解我是如何使用'id' –

1

如果有多個ID的單JSON然後@Rohan庫馬爾的代碼將工作。如果你總是會得到相同的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>'); 
});