本文仅是测试验证使用jsonPath的功能,演示如何使用,详细的介绍可参考GitHub。
测试第一步安装jsonPath:
C:\>pip install Jsonpath
Collecting Jsonpath
Downloading jsonpath-0.82.tar.gz (9.6 kB)
Preparing metadata (setup.py) … done
Using legacy ‘setup.py install’ for Jsonpath, since package ‘wheel’ is not installed.
Installing collected packages: Jsonpath
Running setup.py install for Jsonpath … done
Successfully installed Jsonpath-0.82
https://github.com/json-path/JsonPath
第2步创建用于测试的json文件,依据GitHub示例,稍作修改的测试Json文件如下:
{“store”: {
“book”: [
{“category”: “编程”,
“author”: “Dinnes”,
“title”: “C#高级编程”,
“price”: 99.95
},
{“category”: “编程”,
“author”: “Ritchie”,
“title”: “WPF编程宝典”,
“price”: 12.99
},
{“category”: “编程”,
“author”: “Jeff”,
“title”: “Python爬虫”,
“isbn”: “0-553-21311-3”,
“price”: 78.99
},
{“category”: “编程”,
“author”: “Jack”,
“title”: “Python数据分析”,
“isbn”: “0-395-19395-8”,
“price”: 89.99
}
],
“bicycle”: {
“author”: “法拉利”,
“color”: “黑色”,
“price”: 1999.95
}
}
}
文件名为Demo.json
第3步测试功能:
导入库
import json
import jsonpath
jsObj = json.load(open(“Demo.json”, “r”, encoding=‘utf-8’))
测试1:获取所有书的作者
authors = jsonpath.jsonpath(jsObj, ‘$.store.book[*].author’)
print(authors)
输出:
[‘Dinnes’, ‘Ritchie’, ‘Jeff’, ‘Jack’]
测试2:获取所有的作者,任何包含作者的元素
authors = jsonpath.jsonpath(jsObj, ‘$…author’)
print(authors)
输出:
[‘Dinnes’, ‘Ritchie’, ‘Jeff’, ‘Jack’, ‘法拉利’]
测试3:获取store 下面的所有元素
all_elements = jsonpath.jsonpath(jsObj, ‘$.store.*’)
print(all_elements)
输出:
[[{‘category’: ‘编程’, ‘author’: ‘Dinnes’, ‘title’: ‘C#高级编程’, ‘price’: 99.95}, {‘category’: ‘编程’, ‘author’: ‘Ritchie’, ‘title’: ‘WPF编程宝典’, ‘price’: 12.99}, {‘category’: ‘编程’, ‘author’: ‘Jeff’, ‘title’: ‘Python爬虫’, ‘isbn’: ‘0-553-21311-3’, ‘price’: 78.99}, {‘category’: ‘编程’, ‘author’: ‘Jack’, ‘title’: ‘Python数据分析’, ‘isbn’: ‘0-395-19395-8’, ‘price’: 89.99}], {‘author’: ‘法拉利’, ‘color’: ‘黑色’, ‘price’: 1999.95}]
测试4:获取store 节点下所有商品的价格
all_prices = jsonpath.jsonpath(jsObj, ‘$.store…price’)
print(all_prices)
输出:
[99.95, 12.99, 78.99, 89.99, 1999.95]
测试5:获取第3本书的所有元素
book = jsonpath.jsonpath(jsObj, ‘$…book[2]’)
print(book)
输出:
[{‘category’: ‘编程’, ‘author’: ‘Jeff’, ‘title’: ‘Python爬虫’, ‘isbn’: ‘0-553-21311-3’, ‘price’: 78.99}]
测试6:获取最后一本书的所有元素
last_book = jsonpath.jsonpath(jsObj, ‘$…book[(@.length-1)]’)
print(last_book)
输出:
[{‘category’: ‘编程’, ‘author’: ‘Jack’, ‘title’: ‘Python数据分析’, ‘isbn’: ‘0-395-19395-8’, ‘price’: 89.99}]
测试7:获取前2本书的所有元素
# b_list = jsonpath.jsonpath(jsObj, ‘$…book[0,1]’)? 或者
b_list = jsonpath.jsonpath(jsObj, ‘$…book[0:2]’)
print(b_list)
输出:
[{‘category’: ‘编程’, ‘author’: ‘Dinnes’, ‘title’: ‘C#高级编程’, ‘price’: 99.95}, {‘category’: ‘编程’, ‘author’: ‘Ritchie’, ‘title’: ‘WPF编程宝典’, ‘price’: 12.99}]
测试8:获取包含属性ISBN的元素,即属性过滤
b_isbn = jsonpath.jsonpath(jsObj, ‘$…book[?(@.isbn)]’)
print(b_isbn)
输出:
[{‘category’: ‘编程’, ‘author’: ‘Jeff’, ‘title’: ‘Python爬虫’, ‘isbn’: ‘0-553-21311-3’, ‘price’: 78.99}, {‘category’: ‘编程’, ‘author’: ‘Jack’, ‘title’: ‘Python数据分析’, ‘isbn’: ‘0-395-19395-8’, ‘price’: 89.99}]
测试9:价格过滤
选取价格大于90的元素
b_P = jsonpath.jsonpath(jsObj, ‘$…book[?(@.price >90)]’)
print(b_P)
输出:
[{‘category’: ‘编程’, ‘author’: ‘Dinnes’, ‘title’: ‘C#高级编程’, ‘price’: 99.95}]
|