json 和 string 互转
import json
# 1 json->string
student = {"id": "123", "name": "122", "age": 18}
print(type(student)) # <class 'dict'>
print(student) # {'id': '123', 'name': '122', 'age': 18}
print('-' * 10) # ----------
result = json.dumps(student,ensure_ascii=False) # 中文不乱码
print(type(result)) # <class 'str'>
print(result) # {"id": "123", "name": "122", "age": 18}
# 2 string->json
str = '{"key": "wwww", "word": "qqqq"}'
j = json.loads(str)
print(j) # {'key': 'wwww', 'word': 'qqqq'}
print(type(j)) # <class 'dict'>
参考文档
作者:海马 创建时间:2023-06-06 13:18
最后编辑:海马 更新时间:2024-11-02 21:53
最后编辑:海马 更新时间:2024-11-02 21:53