snack - JsonPath 应用参考
1、JsonPath 应用参考
查询
ONode oNode = ONode.ofBean(store);
oNode.select("$..book[?@.tags contains 'war'].first()").toBean(Book.class); //RFC9535 规范,可以没有括号
oNode.select("$..book[?(!(@.category == 'fiction') && @.price < 40)].first()").toBean(Book.class);
oNode.select("$.store.book.count()");
生成
ONode.ofJson(store).create("$.store.book[0].category").toJson();
删除
ONode.ofBean(store).delete("$..book[-1]");
2、语法简单参考(后续补充完整的)
语法 | 描述 | 示例 |
---|---|---|
$ | 根节点定位 | $..a |
.. | 深度遍历 | $..a |
* | 展开所有子节点 | $..* , $.user.* |
.name , ['name'] | 属性定位 | $.user.name , $['user']['name'] |
[index] | 索引定位 | |
[index,index,index] | 多索引定位 | $.data.list[1,4] |
[start:end:step?] | 区间定位(step 默认为 1) | $.data.list[1:4] |
[?...] | 条表式过滤 | $..b[?(@.c == 12)] $[?(@.c =~ /a+/)] $.data.list[?(@ in $..ary2[0].a)] |
3、内置的操作符与函数
操作符
操作符 | 描述 | 示例 |
---|---|---|
startsWith | 开始为 | $.list[?(@.mobile startsWith '1861111')] |
endsWith | 结尾为 | $.list[?(@.mobile endsWith '4444')] |
contains | 包含 | $.list[?(@.mobile contains '1919')] |
in | 在 | $.list[?(@.label in [1,2])] |
nin | 不在 | $.list[?(@.label nin [1,2])] |
"=~ | 匹配 | $.list[?(@.name =~ /a+/)] |
== | 等于 | $.list[?(@.score == 12)] |
!= | 不等于 | $.list[?(@.score != 12)] |
> | 大于 | $.list[?(@.score > 12)] |
< | 小于 | $.list[?(@.score < 12)] |
>= | 大于等于 | $.list[?(@.score >= 12)] |
<= | 小于等于 | $.list[?(@.score <= 12)] |
函数
函数 | 描述 | 示例 |
---|---|---|
- 聚合函数 | ||
min | 最小值 | $.list.min() |
max | 最大值 | $.list.max() |
avg | 平均值 | $.list.avg() |
sum | 求和 | $.list.sum() |
- 集合函数 | ||
size | 取大小 | $.list.size() |
keys | 取主键 | $.map.keys() |
first | 取第一个值 | $.list.first() |
last | 取第二个值 | $.list.last() |
- 字符串函数 | ||
length | 长度 | $.name.length() |
upper | 变大写 | $.name.upper() |
lower | 变小写 | $.name.lower() |
trim | 去掉头尾的空 | $.name.trim() |