python中将字符串拼接成json
时间: 2023-10-23 18:40:38 浏览: 151
python将字符串转换成json的方法小结
可以使用Python中的json.dumps()函数将Python字符串转换为JSON格式的字符串,然后将两个JSON格式的字符串进行拼接。例如:
import json
data1 = {"name": "Tom", "age": 18}
data2 = {"country": "China", "city": "Beijing"}
json_str1 = json.dumps(data1)
json_str2 = json.dumps(data2)
json_str = json_str1[:-1] + "," + json_str2[1:]
print(json_str)
输出结果为:
{"name": "Tom", "age": 18,"country": "China", "city": "Beijing"}
阅读全文