简体   繁体   English

如何从Python中的JSON文件中获取特定元素?

[英]How to get specific element from a JSON file in Python?

I am using The Guardian's API to get JSON data and now I want a specific element from the JSON data entry. 我正在使用The Guardian的API来获取JSON数据,现在我想要JSON数据条目中的特定元素。 How do I do that? 我怎么做?

I am making the following GET request 我正在提出以下GET请求

url = 'http://content.guardianapis.com/search?from-date=2016-01-02&to-date=2016-01-02&order-by=newest&show-fields=all&page-size=1&api-key=API-KEY.

And now I want webTitle from the JSON data. 现在我想从JSON数据中获取webTitle。 So I am doing as follows: 所以我做如下:

response = requests.get(url)
response = response.content
response = json.loads(response)
content = response['response']['results']['webTitle']

But I am getting TypeError: string indices must be integers, not str error. 但是我收到TypeError: string indices must be integers, not str错误。

However, content = response['response']['results'] works correctly. 但是, content = response['response']['results']正常工作。

Here is a spanshot of JSON data. 这是JSON数据的快照。 在此处输入图片说明

response = request.get(url).json()
content = response['response']['results'][index]['webTitle']

Edit 编辑

Yes as Sudheesh Singanamalla suggests, results is a list and you You'd have to access a given index to get corresponding webTitle 是的,正如Sudheesh Singanamalla所建议的那样,结果是一个列表,您必须访问给定索引才能获取相应的webTitle

We get this error when we mistake a list with a dictionary. 当我们用字典将列表错误时,就会出现此错误。 Seems like 'results' is actually a list with dictionary as its first item. 似乎“结果”实际上是一个列表,其中以字典为第一项。 Do the following instead. 请执行以下操作。

content = response['response']['results'][0]['webTitle']

And obviously I assumed you have one item in the 'results' list only. 很显然,我假设您只有“结果”列表中的一项。 If it has multiple items, iterate over the list. 如果有多个项目,请遍历列表。

response['response']['results'] provides you with a list of dictionaries. response['response']['results']为您提供字典列表。 [{},{},...] where each dictionary contains the following in your example: [{},{},...] ,其中每个字典在您的示例中都包含以下内容:

{id:'', type:'', sectionid:'', .... }

If you want the list of all the webTitle in the results you'd have to iterate over the list obtained from response['response']['results'] and select the webTitle or any other required key in the resulting dictionary. 如果想要结果中所有webTitle的列表,则必须遍历从response['response']['results']获得的列表,然后在结果字典中选择webTitle或任何其他必需的键。 Here's an example 这是一个例子

webTitleList = []
# Make the request for the JSON
results = response['response']['results']
for result in results:
    webTitleList.append(result['webTitle'])
# Now the webTitleList contains the list of all the webTitles in the returned json

However, you can also simplify the selection of the webTitle by using list comprehension on results . 但是,您还可以通过使用对results列表理解来简化webTitle的选择。 There are numerous examples on StackOverflow for the same. 关于StackOverflow的例子很多。

results is a list. results是一个列表。 Iterate over it to fetch the required value. 对其进行迭代以获取所需的值。

Ex: 例如:

for i in response['response']['results']:
    print(i['webTitle'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM