Serialization (json.dumps()) and deserialization (json.loads()) in python

Serialization: the process of converting python data types into str types (dict—>str)

Deserialization: Convert the str type into the data structure of the python data type (str—>dict)

  • Dictionary serialization and deserialization process
dict1 = {
    
    
	"code": "1",
	"msg": "供货厂商列表",
	"data":
		{
    
    
			"stores_id": 1,
			"stores_name": "厂商用户1"
		}
}

# 序列化: dict--->str
dict_str = json.dumps(dict1)
print(dict_str, type(dict_str))
# 打印结果:
{
    
    "code": "1", "msg": "\u4f9b\u8d27\u5382\u5546\u5217\u8868", "data": {
    
    "stores_id": 1, "stores_name": "\u5382\u5546\u7528\u62371"}} <class 'str'>

# 反序列化:str--->dict

str_dict = json.loads(dict_str)
print(str_dict, type(str_dict))
# 打印结果:
{
    
    'code': '1', 'msg': '供货厂商列表', 'data': {
    
    'stores_id': 1, 'stores_name': '厂商用户1'}} <class 'dict'>

Guess you like

Origin blog.csdn.net/weixin_42760923/article/details/106878988