当前位置:   article > 正文

Python中json的encode和decode以及dumps 和load_python json dump encode

python json dump encode

将对象序列化为JSON对象,即对对象进行json encode编码,使用函数,以及使用dumps 将dict 类型转换成json 对象;

而将JSON对象反序列化为一个python类型,即对对象进行json decode解码,和loads性质相同,将json 对象返回为python类型:

示例如下;

  1. import json
  2. data = {"fpindex": "123", "nsInstanceId": "test", "context": "create sfc", "sdnControllerId": "123"}
  3. print("data:%s--------------------", data)
  4. print("type_data: %s----------------", type(data))
  5. data_encode = json.JSONEncoder().encode(data)
  6. print("data_encode: %s------------------",data_encode)
  7. print("type_data_encode:%s ---------------------", type(data_encode))
  8. data_dumps = json.dumps(data)
  9. print("data_dumps:------------------%s ", data_dumps)
  10. print("type_data_dumps:%s --------------------", type(data_dumps))
  11. print(data_encode == data_dumps)

结果如下:

  1. data_encode: %s------------------ {"fpindex": "123", "nsInstanceId": "test", "context": "create sfc", "sdnControllerId": "123"}
  2. type_data_encode:%s --------------------- <class 'str'>
  3. data_dumps:------------------%s {"fpindex": "123", "nsInstanceId": "test", "context": "create sfc", "sdnControllerId": "123"}
  4. type_data_dumps:%s -------------------- <class 'str'>
  5. True

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/215245
推荐阅读
相关标签
  

闽ICP备14008679号