python - 使用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
}}
你能告诉我怎么做吗
解决方案
如果json1和json2的类型是 dict 你可以使用
json1['result'] = json2
但如果它们一开始是字符串,则必须对json1和json2 使用 json.loads并使用提到的代码来更新值。
import json
json.loads(json1)
json.loads(json2)
json1['result'] = json2
推荐阅读
- java - 我的 while 循环和 switch 语句出现语法错误,我不知道为什么
- r - 如何根据另一个数据框中的列表比较和连接两个数据框
- django - 为什么 pre_save 信号不适用于模型文件以外的其他文件?
- c# - 无法转换类型为“Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope”的对象
- python - 在 Ubunto 18.04 LTS 上安装旧的 MYSQL-Python
- sql - Where 和 Join on Id 的区别
- java - 数组的等于方法实现 - Java
- tensorflow - TF 2.0:如何转换 'tf.contrib.eager.num_gpus()'?
- docker - Docker swarm 不会在集群中分发容器
- google-apps-script - 使用带有以“@import.calendar.google.com”结尾的日历 ID 的 getCalendarById()