json-path 组件使用
引入依赖
<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]
支持数组切片语法 -
*
作为通配符,匹配所有成员 -
..
子递归通配符,匹配成员的所有子元素 -
(<expr>)
使用表达式 -
?(<boolean expr>)
进行数据筛选
示例
{ "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 } } }
获取bicycle的price
JsonPath 表达式:$.store.bicycle.price
String read = JsonPath.parse(jsonString).read("$.store.bicycle.price", String.class); System.out.println(read);
结果:
19.95
所有book的author节点
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"]
所有author节点
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 }
store下的所有节点,book数组和bicycle节点
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 } ]
store下的所有price节点
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]
匹配第3个book节点
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}]
匹配倒数第1个book节点
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}]
匹配前两个book节点
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}]
book节点下过滤含isbn字段的节点
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 } ]
与xPath对比
XPath | JsonPath | Result |
---|---|---|
/store/book/author |
$.store.book[*].author |
所有book的author节点 |
//author |
$..author |
所有author节点 |
/store/* |
$.store.* |
store下的所有节点,book数组和bicycle节点 |
/store//price |
$.store..price |
store下的所有price节点 |
//book[3] |
$..book[2] |
匹配第3个book节点 |
//book[last()] |
$..book[(@.length-1)] ,或 $..book[-1:] |
匹配倒数第1个book节点 |
//book[position()<3] |
$..book[0,1] ,或 $..book[:2] |
匹配前两个book节点 |
//book[isbn] |
$..book[?(@.isbn)] |
过滤含isbn字段的节点 |
//book[price<10] |
$..book[?(@.price<10)] |
过滤price<10 的节点 |
//* |
$..* |
递归匹配所有子节点 |
提示:
1.返回对象,默认是 LinkedHashMap ;一般情况不会自动转换为具体对象;
2.返回集合,默认是JSONArray
原创文章,作者:6024010,如若转载,请注明出处:https://blog.ytso.com/243801.html