赞
踩
将对象序列化为JSON对象,即对对象进行json encode编码,使用函数,以及使用dumps 将dict 类型转换成json 对象;
而将JSON对象反序列化为一个python类型,即对对象进行json decode解码,和loads性质相同,将json 对象返回为python类型:
示例如下;
- import json
-
- data = {"fpindex": "123", "nsInstanceId": "test", "context": "create sfc", "sdnControllerId": "123"}
- print("data:%s--------------------", data)
- print("type_data: %s----------------", type(data))
- data_encode = json.JSONEncoder().encode(data)
- print("data_encode: %s------------------",data_encode)
- print("type_data_encode:%s ---------------------", type(data_encode))
-
- data_dumps = json.dumps(data)
- print("data_dumps:------------------%s ", data_dumps)
- print("type_data_dumps:%s --------------------", type(data_dumps))
- print(data_encode == data_dumps)
结果如下:
- data_encode: %s------------------ {"fpindex": "123", "nsInstanceId": "test", "context": "create sfc", "sdnControllerId": "123"}
- type_data_encode:%s --------------------- <class 'str'>
- data_dumps:------------------%s {"fpindex": "123", "nsInstanceId": "test", "context": "create sfc", "sdnControllerId": "123"}
- type_data_dumps:%s -------------------- <class 'str'>
- True
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。