Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:替换嵌套的dict键';在整个json中的值,无论是在列表中还是直接在dict中_Python_Json_Python 3.x_List_Dictionary - Fatal编程技术网

Python:替换嵌套的dict键';在整个json中的值,无论是在列表中还是直接在dict中

Python:替换嵌套的dict键';在整个json中的值,无论是在列表中还是直接在dict中,python,json,python-3.x,list,dictionary,Python,Json,Python 3.x,List,Dictionary,我想将json中的值替换为dictionary的键,它可以直接在json中显示为dictionary,也可以在另一个列表中显示为dictionary,如下所示: { "appType": "popper", "createdAt": "1970-01-01T00:00:00.000Z", "updatedAt": "1970-01-01T00:00:00.000Z"

我想将json中的值替换为dictionary的键,它可以直接在json中显示为dictionary,也可以在另一个列表中显示为dictionary,如下所示:

{ 
  "appType": "popper",
  "createdAt": "1970-01-01T00:00:00.000Z",
  "updatedAt": "1970-01-01T00:00:00.000Z",
  "people": [{
            "name": "Vol1",
            "label": "Vol1",
            "peopleInfo": [{
                "name": "ram",
                "age": "2407653459860",
                "id": "1b738651-da9f-4c85-88c1-70dbfe1976681"
            }],
            "itemInfo": {
                "id": "ee763970-51e2-57a5-955c-d72fc3e28a3f",
                "name": "xyz",
                "type": "any",
                "managed": False
            }
        }],
  "itemInfo": [{
            "managed": False,
            "vendorName": "any",
            "serialNumber": "AF-10124"
             }],
}
期望输出:

{ 
  "appType": "popper",
  "createdAt": "1970-01-01T00:00:00.000Z",
  "updatedAt": "1970-01-01T00:00:00.000Z",
  "peopleInfo": [{
            "name": "Vol1",
            "label": "Vol1",
            "people": [{
                "name": "ram",
                "age": "2407653459860",
                "id": "1b738651-da9f-4c85-88c1-70dbfe1976681"
            }],
            "itemInfo": {
                "id": "ee763970-51e2-57a5-955c-d72fc3e28a3f",
                "name": "xyz",
                "type": "any",
                "managed": True
            }
        }],
  "itemInfo": [{
            "managed": True,
            "vendorName": "any",
            "serialNumber": "AF-10124"
             }],
}
因此,正如在所需的输出中一样,我希望在json中直接将itemInfomanaged标记从false更新为True,并使用python将peopleInfo列表中的itemInfo替换为True。iteminfo字典也可以存在于一些不同列表中的整个json中。谢谢你的帮助

我已经编写了以下代码,但无法使其成为通用代码:

i[“详细信息”][“存储系统信息”][0][“托管”]=True

更新2:

按照您的要求,仅更改“itemInfo”字典中的“托管”字段。 (这不是漂亮或干净的代码)

更新: (这是我的第一个答案,花了我一段时间,希望是好的:))

在复杂的dict/list/“这两者的奇怪组合”中遍历所有字段,查找名为“managed”的字段并将其更改为True。 根据我的回答:

原始答复:

x["people"][0]["itemInfo"]["managed"] = True
x["itemInfo"][0]["managed"] = True

如果有帮助,请投票并标记我的答案这很好,谢谢你的帮助,但我正在寻找一个通用解决方案,其中iteminfo可以在json中的任何位置出现,而不是在固定键中,因此基本上peopleInfo键可以在json中有所不同。更新后的答案很好,谢谢,但我只需要itemInfo字典的托管标志,而不是所有托管标志都需要更新。只是itemInfo dict/list的一个。而且名称iteminfo也可以是任何其他名称:)
# Python 3
def change_this_key_to_true(key, var):
    if hasattr(var,'items'):
        for k, v in var.items():
            if k == key:
                var[k] = True
            if isinstance(v, dict):
                change_this_key_to_true(key, v)
            elif isinstance(v, list):
                for d in v:
                    change_this_key_to_true(key, d)

change_this_key_to_true("managed", myJson)
print(myJson)

x["people"][0]["itemInfo"]["managed"] = True
x["itemInfo"][0]["managed"] = True