JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它使得人们很容易的进行阅读和编写。同时也方便了机器进行解析和生成。适用于进行数据交互的场景,比如网站前台与后台之间的数据交互。
其中类文件对象的理解:
具有read()或者write()方法的对象就是类文件对象,比如f = open(“a.txt”,”r”) f就是类文件对象
具体使用方法:
#json.dumps 实现python类型转化为json字符串
#indent实现换行和空格
#ensure_ascii=False实现让中文写入的时候保持为中文
json_str = json.dumps(mydict,indent=2,ensure_ascii=False)#json.loads 实现json字符串转化为python的数据类型
my_dict = json.loads(json_str)#json.dump 实现把python类型写入类文件对象
with open(","w") as f:json.dump(mydict,f,ensure_ascii=False,indent=2)# json.load 实现类文件对象中的json字符串转化为python类型
with open(","r") as f:my_dict = json.load(f)
用来解析多层嵌套的json数据;JsonPath 是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,提供多种语言实现版本,包括:Javascript, Python, PHP 和 Java。
安装方法:pip install jsonpath官方文档:
{ "store": {"book": [ { "category": "reference","author": "Nigel Rees","title": "Sayings of the Century","price": 8.95},{ "category": "fiction","author": "Evelyn Waugh","title": "Sword of Honour","price": 12.99},{ "category": "fiction","author": "Herman Melville","title": "Moby Dick","isbn": "0-553-21311-3","price": 8.99},{ "category": "fiction","author": "J. R. R. Tolkien","title": "The Lord of the Rings","isbn": "0-395-19395-8","price": 22.99}],"bicycle": {"color": "red","price": 19.95}}
}
JSONPath | Result |
---|---|
$.store.book[*].author | store中的所有的book的作者 |
$…author | 所有的作者 |
$.store.* | store下的所有的元素 |
$.store…price | store中的所有的内容的价格 |
$…book[2] | 第三本书 |
$…book[(@.length-1)] | $…book[-1:] | 最后一本书 |
$…book[0,1] | $…book[:2] | 前两本书 |
$…book[?(@.isbn)] | 获取有isbn的所有数 |
$…book[?(@.price<10)] | 获取价格大于10的所有的书 |
$…* | 获取所有的数据 |
我们以拉勾网城市JSON文件 .json 为例,获取所有城市。
import requests
import jsonpath
import jsonurl = '.json'
response (url)
html_str = t.decode()# 把json格式字符串转换成python对象
jsonobj = json.loads(html_str)# 从根节点开始,匹配name节点
citylist = jsonpath.jsonpath(jsonobj,'$..name')fp = open('city.json','w')content = json.dumps(citylist, ensure_ascii=False)fp.de('utf-8'))
fp.close()
效果如下:
本文发布于:2024-02-01 08:21:39,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170674689935190.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |