<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
JsonPath语法要点:
$ 表示文档的根元素
@ 表示文档的当前元素
.node_name 或 ['node_name'] 匹配下级节点
[index] 检索数组中的元素
[start:end:step] 支持数组切片语法
* 作为通配符,匹配所有成员
.. 子递归通配符,匹配成员的所有子元素
() 使用表达式
?()进行数据筛选
{
"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
}
}
}
JsonPath表达式:$.store.bicycle.price
String read = JsonPath.parse(jsonString).read("$.store.bicycle.price", String.class);
System.out.println(read);
结果:
19.95
JsonPath表达式:$.store.book[*].author
List<String> read = JsonPath.parse(jsonString).read("$.store.book[*].author", new TypeRef<List<String>>() {});
结果:
["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
JsonPath表达式: $..author
List<JSONObject> read = JsonPath.parse(jsonString).read("`$..author`", new TypeRef<List<JSONObject>>() {});
结果:
{
"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
}
JsonPath表达式:$.store.*
List<JSONObject> read = JsonPath.parse(jsonString).read("$.store.*", new TypeRef<List<JSONObject>>() {});
结果:
[
[
{
"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
}
],
{
"color":"red",
"price":19.95
}
]
JsonPath表达式:$.store..price
List<JSONObject> read = JsonPath.parse(jsonString).read("$.store..price", new TypeRef<List<JSONObject>>() {});
结果:
[8.95,12.99,8.99,22.99,19.95]
JsonPath表达式:$..book[2]
List<JSONObject> read = JsonPath.parse(jsonString).read("$..book[2]", new TypeRef<List<JSONObject>>() {});
结果:
[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]
JsonPath表达式:$..book[-1:]
List<JSONObject> read = JsonPath.parse(jsonString).read("$..book[-1:]", new TypeRef<List<JSONObject>>() {});
结果:
[{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]
JsonPath表达式:$..book[0,1],或 $..book[:2]
List<JSONObject> read = JsonPath.parse(jsonString).read("$..book[0,1]", new TypeRef<List<JSONObject>>() {});
结果:
[{"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}]
JsonPath表达式:$.store.book[*][?(@.isbn)] 不包含 $.store.book[*][?(!@.isbn)]
List<JSONObject> read = JsonPath.parse(jsonString).read("$.store.book[*][?(@.isbn)]", new TypeRef<List<JSONObject>>() {});
结果:
[
{
"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
}
]
过滤book price<10的节点
JsonPath表达式:$..book[?(@.price<10)]
List<JSONObject> read = JsonPath.parse(jsonString).read("$..book[?(@.price<10)]", new TypeRef<List<JSONObject>>() {});
结果:
[
{
"category":"reference",
"author":"Nigel Rees",
"title":"Sayings of the Century",
"price":8.95
},
{
"category":"fiction",
"author":"Herman Melville",
"title":"Moby Dick",
"isbn":"0-553-21311-3",
"price":8.99
}
]
动力节点在线课程涵盖零基础入门,高级进阶,在职提升三大主力内容,覆盖Java从入门到就业提升的全体系学习内容。全部Java视频教程免费观看,相关学习资料免费下载!对于火爆技术,每周一定时更新!如果想了解更多相关技术,可以到动力节点在线免费观看JSONPath视频教程哦!
代码小兵64503-29 11:46
代码小兵87208-06 11:36
代码小兵34507-29 13:19