首页 > 解决方案 > 使用python更新json对象中的json元素

问题描述

这是我在 python 中的 json 对象

json1 = {
  "success":true,
  "message":"",
  "result":[{
    "MarketName":"USDT-BTC"
}]}

json2 = {
"success1":true1
}

我想在 python 中使用 json2 更新 json1 对象中的结果元素

json1 = {
  "success":true,
  "message":"",
  "result":{
"success1":true1
}}

你能告诉我怎么做吗

标签: pythonjson

解决方案


如果json1json2的类型是 dict 你可以使用

json1['result'] = json2

但如果它们一开始是字符串,则必须对json1json2 使用 json.loads使用提到的代码来更新值。

import json
json.loads(json1)
json.loads(json2)
json1['result'] = json2

推荐阅读