簡體   English   中英

遍歷 Python 中的嵌套 JSON

[英]Iterate through nested JSON in Python

js = {
   "status": "ok",
   "meta": {
      "count": 1
   },
   "data": {
      "542250529": [
         {
            "all": {
               "spotted": 438,
               "battles_on_stunning_vehicles": 0,
               "avg_damage_blocked": 39.4,
               "capture_points": 40,
               "explosion_hits": 0,
               "piercings": 3519,
               "xp": 376586,
               "survived_battles": 136,
               "dropped_capture_points": 382,
               "damage_dealt": 783555,
               "hits_percents": 74,
               "draws": 2,
               "battles": 290,
               "damage_received": 330011,
               "frags": 584,
               "stun_number": 0,
               "direct_hits_received": 1164,
               "stun_assisted_damage": 0,
               "hits": 4320,
               "battle_avg_xp": 1299,
               "wins": 202,
               "losses": 86,
               "piercings_received": 1004,
               "no_damage_direct_hits_received": 103,
               "shots": 5857,
               "explosion_hits_received": 135,
               "tanking_factor": 0.04
            }
         }
      ]
   }
}

讓我們將此 json 命名為“js”作為變量,該變量將在 for 循環中。 為了更好地理解我在這里所做的事情,我正在嘗試從游戲中收集數據。 這個游戲有數百個不同的坦克,每個坦克都有 tank_id,我可以將 tank_id 發布到游戲服務器並將性能數據響應為“js”。 for tank_id: json = requests.post(tank_id) etc...並將所有這些值提取到我的數據庫中,如屏幕截圖所示。 數據庫

我的 python 代碼:

def api_get():
 for property in js['data']['542250529']['all']:
   spotted = property['spotted']
   battles_on_stunning_vehicles = property['battles_on_stunning_vehicles']
   # etc
   # ...
   insert_to_db(spotted, battles_on_stunning_vehicles, etc....)

例外是:

for property in js['data']['542250529']['all']:
 TypeError: list indices must be integers or slices, not str

什么時候:

print(js['data']['542250529'])

我得到 js 的 rest 作為字符串,我無法迭代...項目'所有'......,任何幫助將不勝感激

您只是錯過了[0]以獲取列表中的第一項:

def api_get():
   for property in js['data']['542250529'][0]['all']:
       spotted = property['spotted']
# ...

仔細查看源碼 JSON 中的數據結構。

有一個包含字典的列表,其鍵為all 所以你需要使用js['data']['542250529'][0]['all']而不是js['data']['542250529']['all'] 然后您可以使用.items()來獲取鍵值對。

見下文。

js = {
   "status": "ok",
   "meta": {
      "count": 1
   },
   "data": {
      "542250529": [
         {
            "all": {
               "spotted": 438,
               "battles_on_stunning_vehicles": 0,
               "avg_damage_blocked": 39.4,
               "capture_points": 40,
               "explosion_hits": 0,
               "piercings": 3519,
               "xp": 376586,
               "survived_battles": 136,
               "dropped_capture_points": 382,
               "damage_dealt": 783555,
               "hits_percents": 74,
               "draws": 2,
               "battles": 290,
               "damage_received": 330011,
               "frags": 584,
               "stun_number": 0,
               "direct_hits_received": 1164,
               "stun_assisted_damage": 0,
               "hits": 4320,
               "battle_avg_xp": 1299,
               "wins": 202,
               "losses": 86,
               "piercings_received": 1004,
               "no_damage_direct_hits_received": 103,
               "shots": 5857,
               "explosion_hits_received": 135,
               "tanking_factor": 0.04
            }
         }
      ]
   }
}

for key, val in js['data']['542250529'][0]['all'].items():
    print("key:", key, " val:", val)

#Or this way

for key in js['data']['542250529'][0]['all']:
    print("key:", key, " val:", js['data']['542250529'][0]['all'][key])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM