【问题标题】:Iterating through json object with python help needed [closed]需要python帮助遍历json对象[关闭]
【发布时间】:2019-08-15 17:59:08
【问题描述】:

我正在尝试在 python 中的 JSON 对象中搜索一个值,但看不到让它做我想做的事

JSON 数据如下所示

{"2103": 
  {"birth_state":null,
  "birth_city":null,
  "first_name":"Cody",
  "position":"OT",
  "depth_chart_order":null,
  "hashtag":"#CodyBooth-NFL-FA-60",
  "birth_country":null,
  "rotoworld_id":null,
  "search_last_name":"booth",
  "injury_body_part":null,
  "player_id":"2103",
  "number":60,
  "yahoo_id":27841,
  "age":27,
  "full_name":"Cody Booth",
  "injury_start_date":null,
  "team":null,
  "height":"6'5\"",
  "college":"Temple",
  "birth_date":"1991-04-22",
  "high_school":null,
  "sport":"nfl",
  "injury_notes":null,
  "fantasy_positions":["OL"],
  "rotowire_id":9866,
  "sportradar_id":"4cd4976e-e230-4935-ad3f-c12876a41350",
  "search_first_name":"cody",
  "injury_status":null,
  "search_rank":9999999,
  "stats_id":null,
  "search_full_name":"codybooth",
  "depth_chart_position":null,
  "practice_description":null,
  "news_updated":null,
  "practice_participation":null,
  "fantasy_data_id":16426,
  "espn_id":17054,
  "active":false,
  "years_exp":1,
  "last_name":"Booth",
  "weight":"285",
  "status":"Inactive",
  "gsis_id":null
},
"6250": 
  {"birth_state":null,
  "birth_city":null,
  "first_name":"Eurndraus",
  "position":"DT",
  "depth_chart_order":null,
  "hashtag":"#EurndrausBryant-NFL-LAC-0",
  "birth_country":null,
  "rotoworld_id":null,
  "search_last_name":"bryant",
  "injury_body_part":null,
  "player_id":"6250",
  "number":0,
  "yahoo_id":32538,
  "age":null,
  "full_name":"Eurndraus Bryant",
  "injury_start_date":null,
  "team":"LAC",
  "height":"",
  "college":null,
  "birth_date":null,
  "high_school":null,
  "sport":"nfl",
  "injury_notes":null,
  "fantasy_positions":["DL"],
  "rotowire_id":null,
  "sportradar_id":"9ff46edb-988f-4c8a-ad56-0502808ca1a6",
  "search_first_name":"eurndraus",
  "injury_status":null,
  "search_rank":9999999,
  "stats_id":null,
  "search_full_name":"eurndrausbryant",
  "depth_chart_position":null,
  "practice_description":null,
  "news_updated":null,
  "practice_participation":null,
  "fantasy_data_id":21183,
  "espn_id":3916426,
  "active":true,
  "years_exp":0,
  "last_name":"Bryant",
  "weight":"",
  "status":"Active",
  "gsis_id":null
},
....}

我正在尝试输入与“search_full_name”匹配的字段,因此我需要在每个对象的“search_full_name”字段中搜索匹配项,然后将对象键写入变量。但我尝试的一切都会返回类型错误或其他错误。

这是我正在尝试使用的刺激代码

def get_player_key(search_name, requestor):
players = Players().get_all_players()

found_players = []
for player in players:
    if player['search_full_name'] == search_name:
        found_players.append((player, player['full_name'], player['position'], player['team'], [requestor]))

【问题讨论】:

  • 什么“输入错误或其他错误”?具体一点。
  • 为什么要添加iterator标签?预期的结果是什么?
  • 你在哪里提取json数据?您可以使用json.loads() 将该数据加载到字典中并在字典上执行快速get 以提取您想要的值。否则,您可以显示一条消息,说明不存在任何信息。
  • 你能发布你想要的输出吗?
  • 为了回答一些问题,我尝试了几种不同的方法并得到了几种不同的错误,但它们似乎都围绕着“玩家”而不是 dict 项目。似乎当我为“玩家中的玩家”做事时,它只是抓住了钥匙(即 2103),而不是与该钥匙关联的对象

标签: python json dictionary


【解决方案1】:

我假设您在尝试使用 player['search_full_name'] 时遇到字符串索引不能是整数的错误。如果你想索引到一个字典,你不能使用字符串,而应该使用 get 函数。

这是一个功能代码(假设 json 在文件 info.json 中)

import json
with open("info.json","r") as f:
    t=f.read()
players=json.loads(t);
found_players=[]
for player in players.values():
    if player.get('search_full_name')=='eurndrausbryant':
        found_players.append((player, player.get('full_name'), player.get('position'), player.get('team')))

您可以轻松地将其重新粘贴到您的函数中并使用 player.get('search_full_name')==search_name 而不是我在这里完成的硬编码 :)

【讨论】:

  • 我得到字符串索引不能是整数,但是当我尝试 json.loads() 时,我收到一个错误,说它需要字符串而不是字典,所以它已经是一个字典项。
  • 跳过 json.loads() 的步骤,然后尝试一下。应该工作:)
【解决方案2】:

我可以通过根据当前键设置一个新对象来解决这个问题

def get_player_key(search_name, requestor):
    players = get_all_players()
    found_players = []


    for player_id in players:
         player = players[player_id]
         if player['search_full_name'] ==  search_name:
            #Blah Blah Blah

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 2017-10-04
    • 2015-09-24
    相关资源
    最近更新 更多