【发布时间】: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