JsonPath解析Json數(shù)據(jù)

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 :


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容