GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.runoob.com — Python JSON | 菜鸟教程
Python 基础教程
Python 基础教程
Python 简介
Python 环境搭建
Python 中文编码
Python VS Code
Python 基础语法
Python 变量类型
Python 运算符
Python 条件语句
Python 循环语句
Python While 循环语句
Python for 循环语句
Python 循环嵌套
Python break 语句
Python continue 语句
Python pass 语句
Python Number(数字)
Python 字符串
Python 列表(List)
Python 元组
Python 字典(Dictionary)
Python 日期和时间
Python 函数
Python 模块
Python 文件I/O
Python File 方法
Python 异常处理
Python OS 文件/目录方法
Python 内置函数
Python 高级教程Python 面向对象 Python 正则表达式 Python CGI 编程 Python MySQL Python 网络编程 Python SMTP Python 多线程 Python XML 解析 Python GUI 编程(Tkinter) Python2.x 与 3.x 版本区别 Python IDE Python JSON Python AI 绘画 Python 100例 Python 测验Python JSON本章节我们将为大家介绍如何使用 Python 语言来编码和解码 JSON 对象。 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写。 JSON 函数使用 JSON 函数需要导入 json 库:import json。
json.dumpsjson.dumps 用于将 Python 对象编码成 JSON 字符串。 语法json.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding="utf-8", default=None, sort_keys=False, **kw) 实例以下实例将数组编码为 JSON 格式数据: 实例
#!/usr/bin/python import json data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ] data2 = json.dumps(data) print(data2) 以上代码执行结果为: [{"a": 1, "c": 3, "b": 2, "e": 5, "d": 4}]
使用参数让 JSON 数据格式化输出: 实例
#!/usr/bin/python import json data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ] data2 = json.dumps({'a': 'Runoob', 'b': 7}, sort_keys=True, indent=4, separators=(',', ': ')) print(data2) 以上代码执行结果为: {
"a": "Runoob",
"b": 7
}
python 原始类型向 json 类型的转化对照表:
json.loadsjson.loads 用于解码 JSON 数据。该函数返回 Python 字段的数据类型。 语法json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]]) 实例以下实例展示了Python 如何解码 JSON 对象: 实例
#!/usr/bin/python import json jsonData = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; text = json.loads(jsonData) print(text) 以上代码执行结果为: {u'a': 1, u'c': 3, u'b': 2, u'e': 5, u'd': 4}
json 类型转换到 python 的类型对照表:
更多内容参考:https://docs.python.org/2/library/json.html。 使用第三方库:DemjsonDemjson 是 python 的第三方模块库,可用于编码和解码 JSON 数据,包含了 JSONLint 的格式化及校验功能。 Github 地址:https://github.com/dmeranda/demjson 官方地址:http://deron.meranda.us/python/demjson/ 环境配置在使用 Demjson 编码或解码 JSON 数据前,我们需要先安装 Demjson 模块。本教程我们会下载 Demjson 并安装: $ tar -xvzf demjson-2.2.3.tar.gz $ cd demjson-2.2.3 $ python setup.py install 更多安装介绍查看:http://deron.meranda.us/python/demjson/install JSON 函数
encodePython encode() 函数用于将 Python 对象编码成 JSON 字符串。 语法demjson.encode(self, obj, nest_level=0) 实例以下实例将数组编码为 JSON 格式数据: 实例
#!/usr/bin/python import demjson data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ] json = demjson.encode(data) print(json) 以上代码执行结果为: [{"a":1,"b":2,"c":3,"d":4,"e":5}]
decodePython 可以使用 demjson.decode() 函数解码 JSON 数据。该函数返回 Python 字段的数据类型。 语法demjson.decode(self, txt) 实例以下实例展示了Python 如何解码 JSON 对象: 实例
#!/usr/bin/python import demjson json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; text = demjson.decode(json) print(text) 以上代码执行结果为: {u'a': 1, u'c': 3, u'b': 2, u'e': 5, u'd': 4}
AI 思考中...
点我分享笔记取消
分类导航
Advertisement
| ||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |