Python请求获取JSON

标签 python dictionary iteration

这是我的请求。

h="API-AUTHENTICATION:key:secret"
r=requests.get("https://URL", h)

这是回应: <Response [200]>

如果我打印请求的纯文本 (print(r.text))我明白了:

{
    "status": "OK",
    "data": [
        {
            "sort": 1,
            "parent_name": "Stocktake Demo",
            "timetables": [
                {
                    "active": 1,
                    "from_date": "Nov 01, 2019",
                    "timetable_data": {
                        "monday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "tuesday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "friday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "wednesday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "thursday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "sunday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "saturday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ]
                    },
                    "type": 1,
                    "to_date": ""
                }
            ],
            "name": "Stocktake Food",
            "parent_id": 137585,
            "parent_sort": 73,
            "image": null,
            "id": 137586,
            "description": null
        },
        {
            "sort": 1,
            "parent_name": "Main Category",
            "timetables": [
                {
                    "active": 1,
                    "from_date": "Nov 01, 2019",
                    "timetable_data": {
                        "monday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "tuesday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "friday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "wednesday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "thursday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "sunday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "saturday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ]
                    },
                    "type": 1,
                    "to_date": ""
                }
            ],
            "name": "Main Subcategory",
            "parent_id": 117042,
            "parent_sort": 2,
            "image": null,
            "id": 117043,
            "description": null
        }
    ]
}

如果我这样做:

a=json.loads(r.text)
print(a.keys())
print(a)

我明白了:

dict_keys(['status', 'data'])

如何将其解析为字典并对其进行迭代。现在,当我迭代它时,我只会到达状态和数据字段。我尝试像这样迭代它:

def print_depth(a, start=0):
    for key, value in a.items():
        print(key, start + 1)
        if isinstance(value, dict):
            print_depth(value, start=start + 1)
print_depth(a)

最佳答案

response.json()如果响应包含 valid JSON,将为您提供 json。
您可以使用 response.headers.get('Content-Type')

检查响应负载 header 中的“内容类型”以验证响应是否为 json
import requests
response = requests.get('https://api.github.com')
if response.status_code == 200 and 'application/json' in response.headers.get('Content-Type',''):
    print(response.json())

您的响应是一个带有 2 个键 statusdata 的 json,您可以直接从 json 访问它们并迭代 data 值是一个列表

h="API-AUTHENTICATION:key:secret"
r=requests.get("https://URL", h)
if r.json().get('status') == 'OK':
    for item in r.json().get('data'):
        print(item.items())

关于Python请求获取JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59261113/

相关文章:

python - 使用python下载文件

Python - 多处理 - 进程变成僵尸

python - 在大型数据集上运行 Pandas UDF 时出现问题

python - 为什么这个类似 python 的 ruby​​ 脚本不起作用?

c# - For循环在列表框中的第一个项目之后停止迭代

algorithm - 关于内存使用的递归与迭代

java - Spring Bean Map 重复键

html - 如何在 Canvas 上渲染 d3.js map ,使其不模糊

python - 需要解释 Python 中 json 和 dict 的区别

scala - 在scala中将文件的内容存储在不可变的Map中