Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python,从JSON获取特定数据_Python_Json - Fatal编程技术网

Python,从JSON获取特定数据

Python,从JSON获取特定数据,python,json,Python,Json,我正在玩传奇联盟api,不管怎样,我制作了这个非常简单的程序 import requests region = input("Enter a region ") summonerName = input("Enter Summoner Name ") apikey = input("Enter APIKey ") r = requests.get(url="https://" + region + ".api.pvp.net/api/lol/" + region + "/v1.4/summone

我正在玩传奇联盟api,不管怎样,我制作了这个非常简单的程序

import requests
region = input("Enter a region ")
summonerName = input("Enter Summoner Name ")
apikey = input("Enter APIKey ")
r = requests.get(url="https://" + region + ".api.pvp.net/api/lol/" + region + "/v1.4/summoner/by-name/" + summonerName + "?api_key=" + apikey)
print(r.json())
这就是它的回报

{'hiimeric': {'revisionDate': 1478543641000, 'name': 'Hi Im Eric', 'id': 36843151, 'profileIconId': 13, 'summonerLevel': 30}}

所以我现在的问题是,例如,我怎样才能让它只发布'name'或者只发布'name'和'profileIconId'?谢谢

JSON对象实际上只是字典和列表的组合。因此,您可以使用以下命令打印名称和profileIconId:

print(r.json()['hiimeric']['name'])
print(r.json()['hiimeric']['profileIconId'])

Json对象有两种:
dict
list
。在本例中,它是
字典
。如果您想知道显式,请使用如下函数

obj = r.json()
print(type(obj))

您是否假设“hiimeric”值是已知的还是未知的?(到目前为止的答案假设它是已知的。)您确定只返回一个用户的值吗?它只是一个字典,包含另一个字典。也许你想?可能是重复的