1. 介绍
类似于XPath在xml文档中的定位,JsonPath表达式通常是用来路径检索或设置Json的。其表达式可以接受“dot–notation”和“bracket–notation”格式,例如$.store.book[0].title、$[‘store’][‘book’][0][‘title’]
JsonPath是一种简单的方法来提取给定JSON文档的部分内容。 JsonPath有许多编程语言,如Javascript,Python和PHP,Java。JsonPath提供的json解析非常强大,它提供了类似正则表达式的语法,基本上可以满足所有你想要获得的json内容。
github上有它的应用:https://github.com/json-path/JsonPath
2. 操作符
符号 | 描述 |
$ | 查询的根节点对象,用于表示一个json数据,可以是数组或对象 |
@ | 过滤器断言(filter predicate)处理的当前节点对象,类似于java中的this字段 |
* | 通配符,可以表示一个名字或数字 |
.. | 可以理解为递归搜索,Deep scan. Available anywhere a name is required. |
<name> | 表示一个子节点 |
[‘<name>’ (, ‘<name>’)] | 表示一个或多个子节点 |
[<number> (, <number>)] | 表示一个或多个数组下标 |
[start:end] | 数组片段,区间为[start,end),不包含end |
[?(<expression>)] | 过滤器表达式,表达式结果必须是boolean |
3. 函数
可以在JsonPath表达式执行后进行调用,其输入值为表达式的结果。
4. 过滤器
过滤器是用于过滤数组的逻辑表达式,一个通常的表达式形如:[?(@.age > 18)],可以通过逻辑表达式&&或||组合多个过滤器表达式,例如[?(@.price < 10 && @.category == ‘fiction’)],字符串必须用单引号或双引号包围,例如[?(@.color == ‘blue’)] or [?(@.color == “blue”)]。
5. 示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37{ "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 }
JSONPath在线解析器:http://www.atoolbox.net/Tool.php?Id=792
最后
以上就是称心滑板最近收集整理的关于JSONPath(XPath for JSON)解析 JSON教程1. 介绍2. 操作符3. 函数4. 过滤器 5. 示例的全部内容,更多相关JSONPath(XPath内容请搜索靠谱客的其他文章。
发表评论 取消回复