1. json.dumps()
将字典加载为字符串
import json
s = {'key1': 'test'}
print(type(s))
print("***分割线***")
dumps_result = json.dumps(s)
print(type(dumps_result))
输出结果:
<class 'dict'>
***分割线***
<class 'str'>
2. json.loads()
将字符串加载为字典,常用于api请求结果转换,便于后期解析
import json
s = {'key1': 'test'}
# print(type(s))
# print("***分割线***")
dumps_result = json.dumps(s)
print(type(dumps_result))
print("***分割线***")
loads_result = json.loads(dumps_result)
print(type(loads_result))
输出结果:
<class 'str'>
***分割线***
<class 'dict'>