如何使用No Array Name解析python中的JSON

时间:2018-06-06 15:38:06

标签: python json

如何使用No Array Name解析python中的JSON?

我的网络请求返回以下内容,我想循环打印出每个incident_key。任何帮助将不胜感激!

print(set_data_OD)
    [
        {
            "fields": {
                "description": "85300361",
                "occurred_at": "2018-06-01T15:24:22.655Z",
                "incident_key": "93568087",
                "details": "The Detailed incident 2",
                "service_key": "22644f5943d",
                "event_type": "trigger"
            },
            "model": "test.incident",
            "pk": 1
        },
        {
            "fields": {
                "description": "85019988",
                "occurred_at": "2018-06-01T15:14:23.371Z",
                "incident_key": "93289161",
                "details": "The Detailed incident 2",
                "service_key": "22644f5943d",
                "event_type": "trigger"
            },
            "model": "test.incident",
            "pk": 12
        }
    ]

1 个答案:

答案 0 :(得分:0)

如果未解析set_data_OD,则可以先解析它。然后循环遍历解析数组的每个元素:

import json

set_data_OD = """[
        {
            "fields": {
                "description": "85300361",
                "occurred_at": "2018-06-01T15:24:22.655Z",
                "incident_key": "93568087",
                "details": "The Detailed incident 2",
                "service_key": "22644f5943d",
                "event_type": "trigger"
            },
            "model": "test.incident",
            "pk": 1
        },
        {
            "fields": {
                "description": "85019988",
                "occurred_at": "2018-06-01T15:14:23.371Z",
                "incident_key": "93289161",
                "details": "The Detailed incident 2",
                "service_key": "22644f5943d",
                "event_type": "trigger"
            },
            "model": "test.incident",
            "pk": 12
        }
    ]"""
parsed_data = json.loads(set_data_OD)
for object in parsed_data:
    print object["fields"]["incident_key"]