假如你存在如下 JSON 数据:
{ "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 }
JsonPah 示例:
JsonPath | 结果 |
$.store.book[*].author | 获取所有书籍的作者 |
$..author | 获取所有作者 |
$.store.* | 获取所有信息,包括书籍和自行车 |
$.store..price | 获取所有东西的价格,包括书籍和自行车 |
$..book[2] | 获取第三本书 |
$..book[-2] | 获取倒数第二本书 |
$..book[0,1] | 获取前两本书 |
$..book[:2] | 获取从索引 0(含)到索引 2(不含)的所有图书 |
$..book[1:2] | 获取从索引 1(含)到索引 2(不含)的所有书籍 |
$..book[-2:] | 获取最后两本书 |
$..book[2:] | 获取从索引 2(含)到最后的所有书籍 |
$..book[?(@.isbn)] | 获取所有有 ISBN 编号的图书 |
$.store.book[?(@.price < 10)] | 获取店内价格低于 10 的书籍 |
$..book[?(@.price <= $['expensive'])] | 获取店内所有价格小于等于 "expensive" 值的书籍 |
$..book[?(@.author =~ /.*REES/i)] | 获取与 regex 匹配的所有图书(忽略大小写) |
$..* | 获取把所有东西 |
$..book.length() | 获取书籍数量 |