JSON和XML的比较可谓不相上下。
Python 2.7中自带了JSON模块,直接import json就可以利用了。
官方文档:http://docs.python.org/library/json.html

Json在线解析网站:http://www.json.cn/#
JSON
json大略说便是javascript中的工具和数组,以是这两种构培养是工具和数组两种构造,通过这两种构造可以表示各种繁芜的构造
工具:工具在js中表示为{ }括起来的内容,数据构造为 { key:value, key:value, … }的键值对的构造,在面向工具的措辞中,key为工具的属性,value为对应的属性值,以是很随意马虎理解,取值方法为 工具.key 获取属性值,这个属性值的类型可以是数字、字符串、数组、工具这几种。
数组:数组在js中是中括号[ ]括起来的内容,数据构造为 [“Python”, “javascript”, “C++”, …],取值办法和所有措辞中一样,利用索引获取,字段值的类型可以是 数字、字符串、数组、工具几种。
import json
json模块供应了四个功能:dumps、dump、loads、load,用于字符串 和 python数据类型间进行转换。
1. json.loads()
把Json格式字符串解码转换成Python工具 从json到python的类型转化对照如下:
# json_loads.pyimport jsonstrList = '[1, 2, 3, 4]'strDict = '{"city": "北京", "name": "大猫"}'json.loads(strList)# [1, 2, 3, 4]json.loads(strDict) # json数据自动按Unicode存储# {u'city': u'\u5317\u4eac', u'name': u'\u5927\u732b'}
2. json.dumps()实现python类型转化为json字符串,返回一个str工具 把一个Python工具编码转换成Json字符串
从python原始类型向json类型的转化对照如下:
# json_dumps.pyimport jsonimport chardetlistStr = [1, 2, 3, 4]tupleStr = (1, 2, 3, 4)dictStr = {"city": "北京", "name": "大猫"}json.dumps(listStr)# '[1, 2, 3, 4]'json.dumps(tupleStr)# '[1, 2, 3, 4]'# 把稳:json.dumps() 序列化时默认利用的ascii编码# 添加参数 ensure_ascii=False 禁用ascii编码,按utf-8编码# chardet.detect()返回字典, 个中confidence是检测精确度json.dumps(dictStr)# '{"city": "\\u5317\\u4eac", "name": "\\u5927\\u5218"}'chardet.detect(json.dumps(dictStr))# {'confidence': 1.0, 'encoding': 'ascii'}print json.dumps(dictStr, ensure_ascii=False)# {"city": "北京", "name": "大刘"}chardet.detect(json.dumps(dictStr, ensure_ascii=False))# {'confidence': 0.99, 'encoding': 'utf-8'}
chardet是一个非常精良的编码识别模块,可通过pip安装
3. json.dump()
将Python内置类型序列化为json工具后写入文件
# json_dump.pyimport jsonlistStr = [{"city": "北京"}, {"name": "大刘"}]json.dump(listStr, open("listStr.json","w"), ensure_ascii=False)dictStr = {"city": "北京", "name": "大刘"}json.dump(dictStr, open("dictStr.json","w"), ensure_ascii=False)
4. json.load()
读取文件中json形式的字符串元素 转化成python类型
# json_load.pyimport jsonstrList = json.load(open("listStr.json"))print strList# [{u'city': u'\u5317\u4eac'}, {u'name': u'\u5927\u5218'}]strDict = json.load(open("dictStr.json"))print strDict# {u'city': u'\u5317\u4eac', u'name': u'\u5927\u5218'}JsonPath(理解)
JsonPath 是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,供应多种措辞实现版本,包括:Javascript, Python, PHP 和 Java。
JsonPath 对付 JSON 来说,相称于 XPATH 对付 XML。
下载地址:https://pypi.python.org/pypi/jsonpath
安装方法:点击Download URL链接下载jsonpath,解压之后实行python setup.py install
官方文档:http://goessner.net/articles/JsonPath
JsonPath与XPath语法比拟:
Json构造清晰,可读性高,繁芜度低,非常随意马虎匹配,下表中对应了XPath的用法。
示例:我们以拉勾网城市JSON文件 http://www.lagou.com/lbs/getAllCitySearchLabels.json 为例,获取所有城市。
# jsonpath_lagou.pyimport requestsimport jsonpathimport jsonimport chardeturl = 'http://www.lagou.com/lbs/getAllCitySearchLabels.json'response = equests.get(url)html = response.text# 把json格式字符串转换成python工具jsonobj = json.loads(html)# 从根节点开始,匹配name节点citylist = jsonpath.jsonpath(jsonobj,'$..name')print citylistprint type(citylist)fp = open('city.json','w')content = json.dumps(citylist, ensure_ascii=False)print contentfp.write(content.encode('utf-8'))fp.close()