最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Python JSON常用编解码方法代码实例
时间:2020-09-05 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下Python JSON常用编解码方法代码实例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
概念
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写。在日常的工作中,应用范围极其广泛。
使用json函数
使用 JSON 函数需要导入 json 库:import json。函数含义:
源码解析:
# coding= utf-8 #!/usr/bin/python import json import sys data = {"username":"测试","age":16} #jsondata = json.dumps(data,ensure_ascii=False) jsondata = json.dumps(data) print("data convert to json") print type(json) text = json.loads(jsondata) print("json convert to data") print text["username"] print text["age"]
使用第三方库:Demjson
Demjson 是 python 的第三方模块库,可用于编码和解码 JSON 数据,包含了 JSONLint 的格式化及校验功能。
函数定义:
源码解析:
#!/usr/bin/python import demjson data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ] json = demjson.encode(data) print json text = demjson.decode(json) print text
相关文章
- Golang ProtoBuf的基本语法详解 10-20
- Python识别MySQL中的冗余索引解析 10-20
- Python+Pygame绘制小球代码展示 10-18
- Python中的数据精度问题介绍 10-18
- Python随机值生成的常用方法介绍 10-18
- python3解压缩.gz文件分析 09-27