首页 > 解决方案 > 数组循环内的字典

问题描述

给定一个包含以下数据的 json 文件:

[
    {
        "activityId": "Task_5.1.1",
        "activityInstanceId": "Task_5.1.1:8c42f564-cdbe-11ea-8be8-00155d891509",
        "errorMessage": null,
        "errorDetails": null,
        "executionId": "8c42ce53-cdbe-11ea-8be8-00155d891509",
        "id": "8c431c75-cdbe-11ea-8be8-00155d891509",
        "lockExpirationTime": null,
        "processDefinitionId": "Process_5.1.1:1:d1c02af8-cdb8-11ea-8be8-00155d891509",
        "processDefinitionKey": "Process_5.1.1",
        "processInstanceId": "8c42802b-cdbe-11ea-8be8-00155d891509",
        "retries": null,
        "suspended": false,
        "workerId": null,
        "topicName": "AppointIM",
        "tenantId": null,
        "priority": 0,
        "businessKey": "57a4c7e9-fd9f-4d71-a39d-7bd8e112a39a"
    },
    {
        "activityId": "Task_5.1.1",
        "activityInstanceId": "Task_5.1.1:fcad6582-cdb8-11ea-8be8-00155d891509",
        "errorMessage": null,
        "errorDetails": null,
        "executionId": "fcad6581-cdb8-11ea-8be8-00155d891509",
        "id": "fcad6583-cdb8-11ea-8be8-00155d891509",
        "lockExpirationTime": null,
        "processDefinitionId": "Process_5.1.1:1:d1c02af8-cdb8-11ea-8be8-00155d891509",
        "processDefinitionKey": "Process_5.1.1",
        "processInstanceId": "fcacf049-cdb8-11ea-8be8-00155d891509",
        "retries": null,
        "suspended": false,
        "workerId": null,
        "topicName": "AppointIM",
        "tenantId": null,
        "priority": 0,
        "businessKey": "f254eb9e-93a5-48e0-8a8e-a95c9fd17ab3"
    }
]

我试图只保存 businessKey 等于某个值的字典。然后这将显示在模板中。

我可以使用以下方式访问每个项目:items_display = list_task['tasks'][0]

但有时可能多达 1000 套或少至 0 套。

我需要一种方法,首先搜索具有所需值的所有字典:然后丢弃剩余部分,或者将找到的项目保存到可以在模板中访问的新字典中。

我尝试不同的方法等已经失败了超过 4 个小时:

我可以通过使用上面的索引来获取键:值等,但需要一种方法来遍历所有可能的索引数量。

标签: pythonarraysdjangodictionary

解决方案


推荐阅读