1、json简介
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它使得人们很容易的进行阅读和编写。同时也方便了机器进行解析和生成。适用于进行数据交互的场景,比如网站前台与后台之间的数据交互。
JSON和XML的比较可谓不相上下。
Python 2.7中自带了JSON模块,直接importjson就可以使用了。
官方文档:http://docs.python.org/library/json.html
json简单说就是javascript中的对象和数组,所以这两种结构就是对象和数组两种结构,通过这两种结构可以表示各种复杂的结构:
对象:对象在js中表示为{ }括起来的内容,数据结构为 { key:value, key:value, ... }的键值对的结构,在面向对象的语言中,key为对象的属性,value为对应的属性值,所以很容易理解,取值方法为 对象.key 获取属性值,这个属性值的类型可以是数字、字符串、数组、对象这几种。
数组:数组在js中是中括号[ ]括起来的内容,数据结构为 ["Python","javascript", "C++", ...],取值方式和所有语言中一样,使用索引获取,字段值的类型可以是数字、字符串、数组、对象几种。
2、json模块
json模块提供了四个功能:dumps、dump、loads、load,用于字符串 和 python数据类型间进行转换。
#json.loads()--Json转Python对象
import json #json数据类型 str_list = '[1, 2, 3, 4]' print(type(str_list)) print(str_list) #转换成python里面的列表 python_list = json.loads(str_list) # [1, 2, 3, 4] print(type(python_list)) print(python_list)json.dumps()--python转json字符串import json import chardet list_str = [1, 2, 3, 4] tuple_str = (1, 2, 3, 4) dict_str = {"city": "北京", "name": "大猫"} print(type(list_str))#<class 'list'> print(list_str)#[1, 2, 3, 4] str_list = json.dumps(list_str)# '[1, 2, 3, 4]' print(type(str_list))#<class 'str'> print(str_list)#[1, 2, 3, 4] str_tuple = json.dumps(tuple_str) print(type(str_tuple)) print(str_tuple)# '[1, 2, 3, 4]' # 注意:json.dumps() 序列化时默认使用的ascii编码 # 添加参数 ensure_ascii=False 禁用ascii编码,按utf-8编码 # chardet.detect()返回字典, 其中confidence是检测精确度 str_dict = json.dumps(dict_str) print(type(str_dict))#<class 'str'> print(str_dict)#{"name": "\u5927\u732b", "city": "\u5317\u4eac"} #检查默认是用什么编码 encode = chardet.detect(str_dict.encode()) print(encode)#{'encoding': 'ascii', 'confidence': 1.0, 'language': ''} #设置使用utf-8编码 print(json.dumps(dict_str, ensure_ascii=False) )# {"name": "大猫", "city": "北京"} str_dict = json.dumps(dict_str, ensure_ascii=False).encode() encode = chardet.detect(str_dict) print(encode)#{'encoding': 'utf-8', 'confidence': 0.938125, 'language': ''}#json.dump()--Python转json对象且写入文件
import json list_str = [{"city": "北京"}, {"name": "大刘"}] #保存列表到list_str.json文件中,并且以utf-8编码 fw = open("list_str.json","w",encoding="utf-8") json.dump(list_str, fw, ensure_ascii=False) dict_str = {"city": "北京", "name": "大刘"} #保存字典到dict_str.json文件中,并且以utf-8编码 fw = open("dict_str.json","w",encoding="utf-8") json.dump(dict_str,fw , ensure_ascii=False,)#json.load()--json转python类型且读取文件import json f = open("list_str.json","r",encoding="utf-8") strList = json.load(f) print(strList)#[{'city': '北京'}, {'name': '大刘'}] strDict = json.load(open("dict_str.json",encoding="utf-8")) print(strDict)#{'name': '大刘', 'city': '北京'}3. JsonPath
JsonPath 是一种信息抽取类库和XPath类似的规则,是从JSON文档中抽取指定信息的工具,提供多种语言实现版本,包括:Javascript,Python, PHP 和 Java。
JsonPath 对于 JSON 来说,相当于 XPATH 对于 XML。
官方文档:http://goessner.net/articles/JsonPath
Json结构清晰,可读性高,复杂度低,非常容易匹配,下表中对应了XPath的用法。
XPath
JSONPath
描述
/
$
根节点
.
@
现行节点
/
.
or[]
取子节点
..
n/a
取父节点,Jsonpath未支持
//
..
就是不管位置,选择所有符合条件的条件
*
*
匹配所有元素节点
@
n/a
根据属性访问,Json不支持,因为Json是个Key-value递归结构,不需要。
[]
[]
迭代器标示(可以在里边做简单的迭代操作,如数组下标,根据内容选值等)
|
[,]
支持迭代器中做多选。
[]
?()
支持过滤操作.
n/a
()
支持表达式计算
()
n/a
分组,JsonPath不支持