jsonpath
jsonpaht是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,jsonpath对于JSON来说,相当于Xpath对于xml。
- jsonpath和xpath对比
Xpath | JSONPath | 描述 |
---|---|---|
/ | $ | 根节点 |
. | @ | 现行节点 |
. | . or [ ] | 取子节点 |
.. | NA | 取父节点,Jsonpath不支持 |
// | .. | 不管位置,选择所有符合条件节点 |
* | * | 匹配所有元素节点 |
@ | NA | 根据属性访问,Json不支持,因为json是个key-value递归结构,不需要属性访问 |
[ ] | [ ] | 迭代器标示(可进行简单的迭代操作,如数组下标,根据内容选值等) |
| | [, ] | 支持多选迭代操作 |
[ ] | ?() | 支持过滤操作 |
NA | () | 支持表达式计算 |
() | NA | 分组,Jsonpath不支持 |
- jsonpath example,选自官方文档。
1 | { "store": { |
xpath | Jsonpath | result |
---|---|---|
/store/book/author | $.store.book[*].author | the author of all books in the store |
//author | $..author | all authors |
/store/* | $.store.* | all things in store,which are some books and a red bicycle. |
/store//price | $.store..price | the price of everything in the store |
//boook[3] | $..book[2] | the third book |
//book[last()] | $..book[@.length-1] or $..book[-1:] | the last book in order |
//book[position()<3] | $..book[0,1] or $..book[:2] | the first two books |
//book[isbn] | $..book[?(@.isbn)] | filter all books with isbn number |
//book[price<10] | $..book[?(@price<10)] | filter all books cheapier than 10 |
//* | $..* | all elements in XML document. All members of JSON structure. |