【发布时间】:2021-03-21 09:42:08
【问题描述】:
我想将 json 中的值替换为字典的键,它可以直接作为字典出现在 json 中,也可以作为字典出现在另一个列表中,如下所示:
{
"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"
}],
}
所以在所需的输出中,我想将 managed 标志更新/替换为 true 从 false 用于 itemInfo 直接在 json 和 itemInfo 在 peopleInfo 列表中使用 python。 iteminfo 字典也可以存在于某些不同列表中的整个 json 中。谢谢你的帮助。
我已经写了下面的代码,但不能使它成为一个通用的:
i["details"]["storageSystemsInfo"][0]["managed"] = True
【问题讨论】:
标签: python json python-3.x list dictionary