python – python – json

2023-04-12 20:55:55 浏览数 (1)

直接看示例

代码语言:javascript复制
import json
# 1.列表嵌套字典转json
data = [{"a": "1"}, {"b": "2"}, {"c": "3"}]

json_str = json.dumps(data, ensure_ascii=False)

print(f"转换后的内容为 - {json_str}")

# 2.字典转json
data = {"a": "1"}, {"b": "2"}, {"c": "3"}

json_str = json.dumps(data, ensure_ascii=False)

print(f"转换后的内容为 - {json_str}")

# 3.字符串转json

s = '[{"a": "1"}, {"b": "2"}, {"c": "3"}]'

json_str = json.loads(s)

print(f"转换后的内容为 - {json_str}")

0 人点赞