jsonpath的使用
一、使用pip安装jsonpath模块
pip install jsonpath
二、数据提取的使用
#‘$’代表定位根节点,‘.’定位子节点,‘..’定位任何位置的子节点,
from jsonpath import jsonpath
#字典数据
data={
"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
}
},
"expensive": 10
}
#提取价格
print(jsonpath(data,'$..price'))
#打印结果[8.95, 12.99, 8.99, 22.99, 19.95]
#提取所有书本的价格
print(jsonpath(data,'$..book..price'))
#打印结果[8.95, 12.99, 8.99, 22.99]
#获取"bicycle"中的price
print(jsonpath(data,'$..bicycle.price'))
#打印结果为[19.95]
#以上打印出来的结果都是列表