JsonPath解析Json数据
JsonPath to JSON is what XPATH is to XML, a simple way to extract parts of a given document. JsonPath is available in many programming languages such as Javascript, Python and PHP. Now also in Java!Given:
{ "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,
"isbn": "0-553-21311-3"
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
Read
All authors:
List authors = JsonPath.read(json, "$.store.book[*].author");
Author of first book in store:
String author = JsonPath.read(json, "$.store.book[1].author");
All books with category = "reference"
List books = JsonPath.read(json, "$.store.book[?(@.category == 'reference')]");
List books = JsonPath.read(json, "$.store.book[?]", filter(where("category").is("reference")));
All books that cost more than 10 USD
List books = JsonPath.read(json, "$.store.book[?(@.price > 10)]");
List books = JsonPath.read(json, "$.store.book[?]", filter(where("price").gt(10)));
All books that have isbn
List books = JsonPath.read(json, "$.store.book[?(@.isbn)]");
List books = JsonPath.read(json, "$.store.book[?]", filter(where("isbn").exists(true)));
Chained filters
Filter filter = Filter.filter(Criteria.where("isbn").exists(true).and("category").in("fiction", "reference"))
List books = JsonPath.read(json, "$.store.book[?]", filter);
Custom filters
Filter myFilter = new Filter.FilterAdapter>(){
@Override
public boolean accept(Map map) {
return map.containsKey("isbn");
}
};
List books = JsonPath.read(json, "$.store.book[?]", myFilter);
All prices in the document
List prices = JsonPath.read(json, "$..price");
Compiled path
You can pre compile a path and use it multiple times
JsonPath path = JsonPath.compile("$.store.book[*]");
List books = path.read(json);
Assert
Asserts are made with Hamcrest matchers
JsonAssert.with(json).assertThat("$.store.bicycle.color", Matchers.equalTo("red"))
.assertThat("$.store.bicycle.price", Matchers.equalTo(19.95D));
Add some static imports and you get this
with(json).assertThat("$.store.bicycle.color", equalTo("red"))
.assertThat("$.store.bicycle.price", equalTo(19.95D));
The Hamcrest library contains a lot of different matchers and they can often be nested.
with(json).assertThat("$..author", hasItems("Nigel Rees", "Evelyn Waugh"))
.assertThat("$..author", is(collectionWithSize(equalTo(2))));
with(json).assertThat("$.store.book[?(@.category == 'x')]", emptyCollection());
If you don't find the matcher you need, roll your own.
Download
Json-path is available at Maven Central,add below dependency into pom.xml :
【JsonPath解析Json数据】
文章图片
推荐阅读
- Quartz|Quartz 源码解析(四) —— QuartzScheduler和Listener事件监听
- Java内存泄漏分析系列之二(jstack生成的Thread|Java内存泄漏分析系列之二:jstack生成的Thread Dump日志结构解析)
- [源码解析]|[源码解析] NVIDIA HugeCTR,GPU版本参数服务器---(3)
- Android系统启动之init.rc文件解析过程
- 小程序有哪些低成本获客手段——案例解析
- Spring源码解析_属性赋值
- Android下的IO库-Okio源码解析(一)|Android下的IO库-Okio源码解析(一) 入门
- 08_JVM学习笔记_类命名空间解析
- WebSocket|WebSocket 语法解析
- jvm|【JVM】JVM08(java内存模型解析[JMM])