使用python读取嵌套的json数据

时间:2019-06-06 14:07:46

标签: python json

我正在尝试从JSon数据下方读取所有采购订单的所有ID。在下面的示例中,只有2个ID(523fbc6c-359a-49ea-81ea-065e20e4b1db + 77a5e074-5b23-4f85-989d-1b8152982c6e)。任何想法如何实现这一目标?

已经尝试了一些博客和代码片段,但结果还没有:)可以使用下一个示例:

json_data = {
        "purchaseOrder": [
            {
                "id": "523fbc6c-359a-49ea-81ea-065e20e4b1db",
                "installationNumber": "test",
                "peerId": "ac0fd195-cb24-4ced-a5fe-664a25651855",
                "validFrom": "2019-05-28T14:57:21.000+0000",
                "validTo": "2019-05-28T15:57:21.000+0000",
                "originalQuantity": 20,
                "quantity": 0,
                "price": 5,
                "periodInitial": "2019-05-28T15:00:00.000+0000",
                "periodFinal": "2019-05-28T16:00:00.000+0000"
            },
            {
                "id": "77a5e074-5b23-4f85-989d-1b8152982c6e",
                "installationNumber": "test",
                "peerId": "308866ba-90cb-47a7-8c73-589c0f355eb7",
                "validFrom": "2019-05-28T14:57:21.000+0000",
                "validTo": "2019-05-28T15:57:21.000+0000",
                "originalQuantity": 20,
                "quantity": 15,
                "price": 5,
                "periodInitial": "2019-05-28T15:00:00.000+0000",
                "periodFinal": "2019-05-28T16:00:00.000+0000"
            }
        ],
        "salesOrder": [
            {
                "id": "7113f1ee-6980-4447-bf71-75c93a4c5bad",
                "installationNumber": "test",
                "peerId": "308866ba-90cb-47a7-8c73-589c0f355eb7",
                "validFrom": "2019-05-28T14:57:21.000+0000",
                "validTo": "2019-05-28T15:57:21.000+0000",
                "originalQuantity": 20,
                "quantity": 0,
                "price": 5,
                "periodInitial": "2019-05-28T15:00:00.000+0000",
                "periodFinal": "2019-05-28T16:00:00.000+0000"
            }
        ],
        "agreement": [
            {
                "id": "e0f0ea4d4ecb357f443df720c457d8f20bcdc0b9d28b8eaa24a1b6bd80bd3ac50",
                "installationNumber": "test",
                "quantity": 15,
                "price": 5,
                "periodInitial": "2019-05-28T15:00:00.000+0000",
                "periodFinal": "2019-05-28T16:00:00.000+0000",
                "type": "A",
                "status": "",
                "agrPurchOrder": "523fbc6c-359a-49ea-81ea-065e20e4b1db",
                "agrSalesOrder": "7113f1ee-6980-4447-bf71-75c93a4c5bad",
                "creationDate": "2019-06-06T09:42:46.710+0000"
            },
            {
                "id": "e0f0ea4d4ecb357f443df720c457d8f20bcdc0b9d28b8eaa24a1b6bd80bd3ac51",
                "installationNumber": "test",
                "quantity": 5,
                "price": 5,
                "periodInitial": "2019-05-28T15:00:00.000+0000",
                "periodFinal": "2019-05-28T16:00:00.000+0000",
                "type": "A",
                "status": "",
                "agrPurchOrder": "77a5e074-5b23-4f85-989d-1b8152982c6e",
                "agrSalesOrder": "7113f1ee-6980-4447-bf71-75c93a4c5bad",
                "creationDate": "2019-06-06T09:42:46.711+0000"
            }
        ],
        "status": ""
    }

5 个答案:

答案 0 :(得分:1)

使用list comprehensions

result = [x['id'] for x in json_data['purchaseOrder']]

['523fbc6c-359a-49ea-81ea-065e20e4b1db',
 '77a5e074-5b23-4f85-989d-1b8152982c6e']

答案 1 :(得分:1)

使用理解,它将收集采购订单中的所有ID。

print([item['id'] for item in json_data['purchaseOrder']])

Result: ['523fbc6c-359a-49ea-81ea-065e20e4b1db', '77a5e074-5b23-4f85-989d-1b8152982c6e']

答案 2 :(得分:0)

您可以导入JSON数据并遍历“ purchaseOrder”数组,并将每个元素的ID附加到数组中。

答案 3 :(得分:0)

您的json_data变量不是json构造。这是一个python字典。如果要将其作为json数据处理,则首先需要使用json模块加载它。您可以将变量更改为json数据的字符串,如下所示:

import json

json_data = '''
{
        "purchaseOrder": [
            {
                "id": "523fbc6c-359a-49ea-81ea-065e20e4b1db",
                "installationNumber": "test",
                "peerId": "ac0fd195-cb24-4ced-
...
}
'''

loaded_json = json.loads(json_data)
print(loaded_json['purchaseOrder']

然后您可以遍历loaded_json对象并将其作为json对象遍历。

答案 4 :(得分:0)

谢谢。拟议的解决方案非常完美!