您现在的位置是:网站首页> 编程资料编程资料
Python对象与json数据的转换问题实例详解_python_
2023-05-26
241人已围观
简介 Python对象与json数据的转换问题实例详解_python_
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写。
JSON 函数
使用 JSON 函数需要导入 json 库:import json。
| 函数 | 描述 |
|---|---|
| json.dumps | 将 Python 对象编码成 JSON 字符串 |
| json.loads | 将已编码的 JSON 字符串解码为 Python 对象 |
json.dumps
json.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 类型的转化对照表:
| Python | JSON |
|---|---|
| dict | object |
| list, tuple | array |
| str, unicode | string |
| int, long, float | number |
| True | true |
| False | false |
| None | null |
json.loads
json.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 的类型对照表:
| JSON | Python |
|---|---|
| object | dict |
| array | list |
| string | unicode |
| number (int) | int, long |
| number (real) | float |
| true | True |
| false | False |
| null | None |
更多内容参考:https://docs.python.org/2/library/json.html。
使用第三方库:Demjson
Demjson 是 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 函数
| 函数 | 描述 |
|---|---|
| encode | 将 Python 对象编码成 JSON 字符串 |
| decode | 将已编码的 JSON 字符串解码为 Python 对象 |
encode
Python 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}]
decode
Python 可以使用 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}
测试程序:
import json #json_data = [{"苹果":"appale","香蕉":"banana"}, # {"猫":"cat","狗":"dog"}, # {"红色":"red","蓝色":"blue"},] #python数据类型: list json_data = [{"a":"appale","b":"banana"}, {"c":"cat","d":"dog"}, {"r":"red","b":"blue"},] print(json_data) #[{'a': 'appale', 'b': 'banana'}, {'c': 'cat', 'd': 'dog'}, {'r': 'red', 'b': 'blue'}] print(type(json_data))# json_data2 = json.dumps(json_data)#json.dumps 用于将 Python 对象编码成 JSON 字符串。 print(json_data2) #[{"a": "appale", "b": "banana"}, {"c": "cat", "d": "dog"}, {"r": "red", "b": "blue"}] print(type(json_data2))# site = { "sites": [ { "name":"" , "url":"www.jb51.net" }, { "name":"google" , "url":"www.google.com" }, { "name":"微博" , "url":"www.weibo.com" } ] } #print(sites) #NameError: name 'sites' is not defined print(site['sites']) 到此这篇关于Python对象与json数据的转换的文章就介绍到这了,更多相关Python对象转换json数据内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关内容
- Python中Json使用示例详解_python_
- python playwrigh框架入门安装使用_python_
- python playwright之元素定位示例详解_python_
- pycharm2022.1最新永久激活码破解补丁一键安装教程免费分享(2022持续更新)_python_
- Sentry的安装、配置、使用教程(Sentry日志手机系统)_python_
- Python中的 No Module named ***问题及解决_python_
- 利用Python脚本写端口扫描器socket,python-nmap_python_
- Python+pyaudio实现音频控制示例详解_python_
- python高温预警数据获取实例_python_
- Python中的socket网络模块介绍_python_
