Python JsonPath模块
jsonpath介绍
用来解析多层嵌套的json数据;JsonPath 是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,提供多种语言实现版本,包括:Javascript, Python, PHP 和 Java。
jsonpath表达式总是和JSON结构结合在一起的 就如同 XML文档和XPath结合在一起一样。“根元素对象”在JsonPath中被称为
$
无论它是一个对象或数组。
import jsonpath
response = json.loads(reqs)
jpid = jsonpath.jsonpath(response, '$..key_name')
其中:“$”表示最外层的{},“..”表示模糊匹配,当传入不存在的key_name时,程序会返回false
JsonPath表达式可以使用 点号
$.store.book[0].title
或使用 中括号
$['store']['book'][0]['title']
n/a
表not applicable
不适用
JsonPath
XPath
"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
"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