![](/img/trans.png)
[英]For each sub-tree in an AST tree, create a corresponding list using Python
[英]Add/Remove JSON sub-tree in Python
這是我打算在后端使用的空 JSON:
{
"user": "",
"mids": {
"merchant_id": {
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
}
}
}
每個用戶可能有多個商戶 ID。 因此,我需要能夠添加另一個完整的 Mercer_id 子樹,如下所示:
{
"user": "",
"mids": {
"merchant_id": {
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
},
"merchant_id2": {
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
}
}
}
有什么簡單的方法可以在 Python 中添加/刪除 JSON?
非常感謝 !
你可以試試.update()
base = {
"user": "",
"mids": {
"merchant_id": {
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
}
}
}
another_merchant = {"merchant_id2": {
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
}
}
base["mids"].update(another_merchant)
print(base)
{
"user": "",
"mids":
{
"merchant_id":
{
"name": "",
"cruise_credentials":
{
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB":
{
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
},
"merchant_id2":
{
"name": "",
"cruise_credentials":
{
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB":
{
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
}
}
}
非常感謝你的幫助。
有兩種解決方案,具體取決於我們如何處理。
使用 Mercer_id 的 LIST 將允許在第一個 NAME 字典之后簡單地添加一個新字典 NAME。
{
"user": "",
"merchant_id": [
{
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
},
{
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
}
]
}
或者我們可以使用.update() function。 但是,這將迫使我們自己將字段“merchant_name”更改為商家_name2,並且需要額外的邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.