简体   繁体   English

如何使用Python从JSON文件中提取特定字段

[英]How to extract an specific field from a JSON file with Python

I am trying to download some JSON files from an URL and then extract an specific field from this files. 我正在尝试从URL下载一些JSON文件,然后从该文件中提取特定字段。

This is what I have done so far: 到目前为止,这是我所做的:

import requests
r = requests.get('https://api.someURL')
if r.status_code != 200:
    print("Failure")
    break
else:

And here is where I am stucked. 这就是我被困住的地方。 This code is supposed to retreive a JSON-formatted list with a lot of information. 该代码应该检索具有很多信息的JSON格式列表。 Something like this: 像这样:

[
{          
   "id": "a12345",
   "size": "58457888",
   "status":"AVAILABLE",
   "uri": "https://api.output:"output file"
},
{
...
]

I dont know how long the list will be, but what I am trying is to get, for every element in the list, the information stored in the field "uri". 我不知道列表会持续多长时间,但是我正在尝试获取列表中每个元素存储在“ uri”字段中的信息。

¿Any idea of how can I get this? ¿关于我如何得到这个的任何想法?

Sorry if I am asking for too much help, but I am not used to work with JSON files. 抱歉,如果我需要太多帮助,但是我不习惯使用JSON文件。

Thank you very much in advance, Álvaro 预先非常感谢Álvaro

The response object contains a json() method to decode the JSON response into a usable Python object. 响应对象包含一个json()方法,用于将JSON响应解码为可用的Python对象。 Based on your example data it should be a list of dictionaries. 根据您的示例数据,它应该是字典列表。

import requests
r = requests.get('https://api.someURL')
if r.status_code != 200:
    print("Failure")
    break
else:
    data = r.json()
    for record in data:
        uri = record.get('uri')
        print(uri) # or do whatever you need to do with it

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

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