好的,所以我正在为一个传奇联盟开发一个小应用程序,我正在尝试使用他们的api来获取玩家数据。我想展示他们前三名的冠军并告诉他们一些有关他们的信息。我可以做到这一点很好,但我很难通过身份证来取得冠军的名字。我有一个json数组文件,看起来像这样。我只会包括其中一些,因为每个冠军的时间相当长。
[
{
"id": "aatrox",
"key": "266",
"name": "Aatrox",
"title": "the Darkin Blade",
"tags": [
"Fighter",
"Tank"
],
"stats": {
"hp": 580,
"hpperlevel": 85,
"mp": 100,
"mpperlevel": 0,
"movespeed": 345,
"armor": 24.384,
"armorperlevel": 3.8,
"spellblock": 32.1,
"spellblockperlevel": 1.25,
"attackrange": 150,
"hpregen": 6.59,
"hpregenperlevel": 0.5,
"mpregen": 0,
"mpregenperlevel": 0,
"crit": 0,
"critperlevel": 0,
"attackdamage": 60.376,
"attackdamageperlevel": 3.2,
"attackspeedoffset": -0.04,
"attackspeedperlevel": 3
},
"icon": "http://ddragon.leagueoflegends.com/cdn/7.14.1/img/champion/Aatrox.png",
"sprite": {
"url": "http://ddragon.leagueoflegends.com/cdn/7.14.1/img/sprite/champion0.png",
"x": 0,
"y": 0
},
"description": "Aatrox is a legendary warrior, one of only five that remain of an ancient race known as the Darkin. He wields his massive blade with grace and poise, slicing through legions in a style that is hypnotic to behold. With each foe felled, Aatrox's seemingly..."
},
{
"id": "ahri",
"key": "103",
"name": "Ahri",
"title": "the Nine-Tailed Fox",
"tags": [
"Mage",
"Assassin"
],
"stats": {
"hp": 514.4,
"hpperlevel": 80,
"mp": 334,
"mpperlevel": 50,
"movespeed": 330,
"armor": 20.88,
"armorperlevel": 3.5,
"spellblock": 30,
"spellblockperlevel": 0.5,
"attackrange": 550,
"hpregen": 6.508,
"hpregenperlevel": 0.6,
"mpregen": 6,
"mpregenperlevel": 0.8,
"crit": 0,
"critperlevel": 0,
"attackdamage": 53.04,
"attackdamageperlevel": 3,
"attackspeedoffset": -0.065,
"attackspeedperlevel": 2
},
"icon": "http://ddragon.leagueoflegends.com/cdn/7.14.1/img/champion/Ahri.png",
"sprite": {
"url": "http://ddragon.leagueoflegends.com/cdn/7.14.1/img/sprite/champion0.png",
"x": 48,
"y": 0
},
"description": "Unlike other foxes that roamed the woods of southern Ionia, Ahri had always felt a strange connection to the magical world around her; a connection that was somehow incomplete. Deep inside, she felt the skin she had been born into was an ill fit for her..."
},
{
"id": "akali",
"key": "84",
"name": "Akali",
"title": "the Fist of Shadow",
"tags": [
"Assassin"
],
"stats": {
"hp": 587.8,
"hpperlevel": 85,
"mp": 200,
"mpperlevel": 0,
"movespeed": 350,
"armor": 26.38,
"armorperlevel": 3.5,
"spellblock": 32.1,
"spellblockperlevel": 1.25,
"attackrange": 125,
"hpregen": 8.342,
"hpregenperlevel": 0.65,
"mpregen": 50,
"mpregenperlevel": 0,
"crit": 0,
"critperlevel": 0,
"attackdamage": 58.376,
"attackdamageperlevel": 3.2,
"attackspeedoffset": -0.1,
"attackspeedperlevel": 3.1
},
"icon": "http://ddragon.leagueoflegends.com/cdn/7.14.1/img/champion/Akali.png",
"sprite": {
"url": "http://ddragon.leagueoflegends.com/cdn/7.14.1/img/sprite/champion0.png",
"x": 96,
"y": 0
},
"description": "There exists an ancient order originating in the Ionian Isles dedicated to the preservation of balance. Order, chaos, light, darkness -- all things must exist in perfect harmony for such is the way of the universe. This order is known as the Kinkou and..."
},
{
"id": "alistar",
"key": "12",
"name": "Alistar",
"title": "the Minotaur",
"tags": [
"Tank",
"Support"
],
"stats": {
"hp": 613.36,
"hpperlevel": 106,
"mp": 278.84,
"mpperlevel": 38,
"movespeed": 330,
"armor": 24.38,
"armorperlevel": 3.5,
"spellblock": 32.1,
"spellblockperlevel": 1.25,
"attackrange": 125,
"hpregen": 8.678,
"hpregenperlevel": 0.85,
"mpregen": 8.5,
"mpregenperlevel": 0.8,
"crit": 0,
"critperlevel": 0,
"attackdamage": 61.1116,
"attackdamageperlevel": 3.62,
"attackspeedoffset": 0,
"attackspeedperlevel": 2.125
},
"icon": "http://ddragon.leagueoflegends.com/cdn/7.14.1/img/champion/Alistar.png",
"sprite": {
"url": "http://ddragon.leagueoflegends.com/cdn/7.14.1/img/sprite/champion0.png",
"x": 144,
"y": 0
},
"description": "As the mightiest warrior to ever emerge from the Minotaur tribes of the Great Barrier, Alistar defended his tribe from Valoran's many dangers; that is, until the coming of the Noxian army. Alistar was lured from his village by the machinations of Keiran..."
},
我可以通过API获取玩家3最喜欢的冠军的ID,但我需要使用该ID从Json中获取冠军名称。因此,例如,如果玩家最喜欢的冠军id是266我需要一些东西,它将搜索json等于266的键,然后返回json对象,以便我可以从中提取名称。我明白应该发生什么,但却无法提出工作代码来做到这一点。
答案 0 :(得分:0)
提供您的JSON字符串化:
import json
def find_name(player_key, data):
matches = [player['name'] for player in data if player.get('key') == player_key]
return matches[0] if matches else None
json_response = '....'
data = json.loads(json_response)
player_name = find_name(266, data)