Python json使用
转自:https://www.cnblogs.com/wangyayun/p/6699184.html?utm_source=tuicool&utm_medium=referral
使用Python解析JSON详解
为大家介绍如何使用 Python 语言来编码和解码 JSON 对象。
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 } ] json = json.dumps(data)
print json
以上代码执行结果为:
[{"a": 1, "c": 3, "b": 2, "e": 5, "d": 4}]
使用参数让 JSON 数据格式化输出:
>>> import json
>>> print json.dumps({'a': 'Runoob', 'b': 7}, sort_keys=True, indent=4, separators=(',', ': '))
{
"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 |
使用第三方库: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}
Python json使用的更多相关文章
- python json基础学习01
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import json #全称(javascript object ...
- python json数据的转换
1 Python数据转json字符串 import json json_str = json.dumps(py_data) 参数解析: json_str = json.dumps(py_data,s ...
- python大法好——python json
Python JSON 本章节我们将为大家介绍如何使用 Python 语言来编码和解码 JSON 对象. JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式, ...
- Python json 读取 json 文件并转为 dict
Python json 读取 json 文件并转为 dict 在 D 盘 新建 test.json: { "test": "测试\n换行", "dic ...
- Python Json & Pickle模块
用于序列化的两个模块 Json,用于字符串 和 python数据类型间进行转换 Pickle,用于python特有的类型 和 python的数据类型间进行转换 Json模块提供了四个功能:dumps. ...
- Python: json模块实例详解
ref:https://www.jianshu.com/p/e29611244810 https://www.cnblogs.com/qq78292959/p/3467937.html https:/ ...
- python学习笔记——python JSON
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写. 1.JSON 函数 使用 JSON 函数需要导入 json 库:import json ...
- Python -- Json 数据编码及解析
Python -- Json 数据编码及解析 Json 简单介绍 JSON: JavaScript Object Notation(JavaScript 对象表示法) JSON 是存储和交换文本 ...
- python json.dumps() 中文乱码问题
python json.dumps() 中文乱码问题 python 输出一串中文字符,在控制台上(控制台使用UTF-8编码)通过print 可以正常显示,但是写入到文件中之后,中文字符都输出成as ...
随机推荐
- 【刷题】LOJ 6227 「网络流 24 题」最长k可重线段集问题
题目描述 给定平面 \(\text{xoy}\) 上 \(n\) 个开线段组成的集合 \(\text{I}\) ,和一个正整数 \(k\) ,试设计一个算法. 从开线段集合 \(\text{I}\) ...
- 学习Spring Boot:(十二)Mybatis 中自定义枚举转换器
前言 在 Spring Boot 中使用 Mybatis 中遇到了字段为枚举类型,数据库存储的是枚举的值,发现它不能自动装载. 解决 内置枚举转换器 MyBatis内置了两个枚举转换器分别是:org. ...
- Github Desktop 克隆仓库一直出现“Authentication failed”
如图所示: 解决方法: 使用ssh链接下载而不是https
- 解题:LNOI 2014 LCA
题面 这题有点意思 转化问题,我们把询问区间的点到根链加,再查询询问点到根的权值和就是每个询问的答案. 然后如果你数据结构没学傻只需要差分一下就可以扫一遍出解了 #include<cstdio& ...
- centos7下设置opencv环境变量
最近要装YOLO,但是MAKE的时候总是找不到OPENCV的路径, 原因是:我以前卸载过一次OPENCV,然后自己重新安装了opencv2.4.10, 因为当时只在QT 中用,所以编译完也没有设置环 ...
- HDU 6153 扩展kmp
A Secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total ...
- angular模块
深入浅析AngularJS中的模块 模块是AngularJS应用程序的一个组成部分,模块可以是一个Controller.Service服务.Filter过滤器.directive指令,这些都属于模块. ...
- bzoj千题计划209:bzoj1185: [HNOI2007]最小矩形覆盖
http://www.lydsy.com/JudgeOnline/problem.php?id=1185 题解去看它 http://www.cnblogs.com/TheRoadToTheGold/p ...
- SQL语句(八)按条件查询
SELECT * FROM student SELECT sclass, snumb, sname FROM student --物理班有哪些同学 --年龄小于20岁的有哪些同学? --定价在30元以 ...
- [整理]定义但未初始化赋值的局部变量与OXCCCCCCCC
开发环境 : win7 32bit ,VS2010,先看一段C代码: #include <stdio.h> int main(){ int x; //-858993460 printf(& ...