我想将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中直接为itemInfo更新/替换managed标记为True,以及使用python在peopleInfo列表中更新/替换itemInfo。iteminfo字典也可以存在于一些不同列表中的整个json中。谢谢你的帮助
我已经编写了以下代码,但无法使其成为通用代码:
i["details"]["storageSystemsInfo"][0]["managed"] = True
更新2:
按照您的要求,仅更改“itemInfo”字典中的“托管”字段。 (这不是漂亮或干净的代码)
更新: (这是我的第一个答案,花了我一段时间,希望是好的:))
在复杂的dict/list/“这两者的奇怪组合”中遍历所有字段,查找名为“managed”的字段并将其更改为True。 根据我的回答: Find all occurrences of a key in nested dictionaries and lists
原始答复:
相关问题 更多 >
编程相关推荐