書接上文和上上文:
今天分享的內(nèi)容是JSonpath
過濾數(shù)據(jù)的API
。這部分API
分成兩類:一類是運算符,例如:==
、>
、=~
這些,一類是方法或者函數(shù),例如:in
、nin
、anyof
等等。
第一類實在沒啥可分享的寫法都是按照語言使用習(xí)慣,然后之前的文章也都介紹過了,下面主要分享一下方法函數(shù)的使用。
json數(shù)據(jù)
在原來的數(shù)據(jù)基礎(chǔ)上增加了page
和pages
兩個字段。
JSONObject json = JSON.parseObject("{" +
" \"store\": {" +
" \"book\": [" +
" {" +
" \"category\": \"reference\"," +
" \"author\": \"Nigel Rees\"," +
" \"title\": \"Sayings of the Century\"," +
" \"page\": \"D\"," +
" \"pages\": [\"S\",\"X\",\"G\"]," +
" \"price\": 8.95" +
" }," +
" {" +
" \"category\": \"fiction\"," +
" \"author\": \"Evelyn Waugh\"," +
" \"title\": \"Sword of Honour\"," +
" \"page\": \"A\"," +
" \"pages\": [\"A\",\"B\"]," +
" \"price\": 12.99" +
" }," +
" {" +
" \"category\": \"fiction\"," +
" \"author\": \"Herman Melville\"," +
" \"title\": \"Moby Dick\"," +
" \"isbn\": \"0-553-21311-3\"," +
" \"page\": \"B\"," +
" \"pages\": [\"E\",\"F\"]," +
" \"price\": 8.99" +
" }," +
" {" +
" \"category\": \"fiction\"," +
" \"author\": \"J. R. R. Tolkien\"," +
" \"title\": \"The Lord of the Rings\"," +
" \"isbn\": \"0-395-19395-8\"," +
" \"page\": \"C\"," +
" \"pages\": [\"C\",\"D\"]," +
" \"price\": 22.99" +
" }" +
" ]," +
" \"bicycle\": {" +
" \"color\": \"red\"," +
" \"price\": 19.95" +
" }" +
" }," +
" \"expensive\": 10," +
" \"ss\": [32,32,4,23]" +
"}");
字段值在某個數(shù)組中
這里的數(shù)組的寫法跟語言一樣。
jsonpath
:$.store.book[?(@.page in ['A','C'])]
代碼:
Object read = JsonPath.read(json, "$.store.book[?(@.page in ['A','C'])]");
output(JSONArray.parseArray(read.toString()));
等效寫法繼續(xù)省略……
控制臺輸出:
INFO-> 當(dāng)前用戶:fv,IP:10.60.192.21,工作目錄:/Users/fv/Documents/workspace/fun/,系統(tǒng)編碼格式:UTF-8,系統(tǒng)Mac OS X版本:10.15.6
INFO->
~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~ JSON ~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~
> {
> ① . "pages":[
> ② . . . "A",
> ② . . . "B"
> ① . ],
> ① . "author":"Evelyn Waugh",
> ① . "price":12.99,
> ① . "page":"A",
> ① . "category":"fiction",
> ① . "title":"Sword of Honour"
> }
~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~ JSON ~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~
INFO->
~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~ JSON ~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~
> {
> ① . "pages":[
> ② . . . "C",
> ② . . . "D"
> ① . ],
> ① . "author":"J. R. R. Tolkien",
> ① . "price":22.99,
> ① . "isbn":"0-395-19395-8",
> ① . "page":"C",
> ① . "category":"fiction",
> ① . "title":"The Lord of the Rings"
> }
~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~ JSON ~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~
Process finished with exit code 0
字段值不在某個數(shù)組中
這個跟上面的一模一樣,只是函數(shù)變成了nin
,應(yīng)該是no in
的縮寫吧。
jsonpath
:$.store.book[?(@.page nin ['A','C'])]
代碼:
Object read = JsonPath.read(json, "$.store.book[?(@.page nin ['A','C'])]");
output(JSONArray.parseArray(read.toString()));
等效寫法繼續(xù)省略……
控制臺輸出:
INFO-> 當(dāng)前用戶:fv,IP:10.60.192.21,工作目錄:/Users/fv/Documents/workspace/fun/,系統(tǒng)編碼格式:UTF-8,系統(tǒng)Mac OS X版本:10.15.6
INFO->
~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~ JSON ~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~
> {
> ① . "pages":[
> ② . . . "S",
> ② . . . "X",
> ② . . . "G"
> ① . ],
> ① . "author":"Nigel Rees",
> ① . "price":8.95,
> ① . "page":"D",
> ① . "category":"reference",
> ① . "title":"Sayings of the Century"
> }
~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~ JSON ~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~
INFO->
~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~ JSON ~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~
> {
> ① . "pages":[
> ② . . . "E",
> ② . . . "F"
> ① . ],
> ① . "author":"Herman Melville",
> ① . "price":8.99,
> ① . "isbn":"0-553-21311-3",
> ① . "page":"B",
> ① . "category":"fiction",
> ① . "title":"Moby Dick"
> }
~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~ JSON ~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~
Process finished with exit code 0
子集
這個校驗的是數(shù)組之間的關(guān)系,value
的值必需是數(shù)組才行,如果不是,會返回空值,但不會報錯。
jsonpath
:$.store.book[?(@.pages subsetof ['A','B','C'])]
代碼:
Object read = JsonPath.read(json, "$.store.book[?(@.pages subsetof ['A','B','C'])]");
output(JSONArray.parseArray(read.toString()));
等效寫法繼續(xù)省略……
控制臺輸出:
INFO-> 當(dāng)前用戶:fv,IP:10.60.192.21,工作目錄:/Users/fv/Documents/workspace/fun/,系統(tǒng)編碼格式:UTF-8,系統(tǒng)Mac OS X版本:10.15.6
INFO->
~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~ JSON ~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~
> {
> ① . "pages":[
> ② . . . "A",
> ② . . . "B"
> ① . ],
> ① . "author":"Evelyn Waugh",
> ① . "price":12.99,
> ① . "page":"A",
> ① . "category":"fiction",
> ① . "title":"Sword of Honour"
> }
~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~ JSON ~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~
Process finished with exit code 0
數(shù)組之間的校驗
國外的月亮也不一定是真的圓,這個據(jù)說在19年初被支持了,但是最后一個發(fā)行版本是在17年,已經(jīng)三年沒有發(fā)行版本了。我改天研究一下,自己弄個最新的版本版本吧。
屬性值數(shù)量驗證
size
可以驗證數(shù)組長度也可以驗證字符串長度。
jsonpath
:$.store.book[?(@.pages size 3)]
字符串長度:
jsonpath
:$.store.book[?(@.author size 16)]
代碼:
Object read = JsonPath.read(json, "$.store.book[?(@.pages size 3)]");
output(JSONArray.parseArray(read.toString()));
Object read = JsonPath.read(json, "$.store.book[?(@.author size 16)]");
output(JSONArray.parseArray(read.toString()));
等效寫法繼續(xù)省略……
控制臺輸出:
INFO-> 當(dāng)前用戶:fv,IP:10.60.192.21,工作目錄:/Users/fv/Documents/workspace/fun/,系統(tǒng)編碼格式:UTF-8,系統(tǒng)Mac OS X版本:10.15.6
INFO->
~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~ JSON ~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~
> {
> ① . "pages":[
> ② . . . "S",
> ② . . . "X",
> ② . . . "G"
> ① . ],
> ① . "author":"Nigel Rees",
> ① . "price":8.95,
> ① . "page":"D",
> ① . "category":"reference",
> ① . "title":"Sayings of the Century"
> }
~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~ JSON ~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~
Process finished with exit code 0
INFO-> 當(dāng)前用戶:fv,IP:10.60.192.21,工作目錄:/Users/fv/Documents/workspace/fun/,系統(tǒng)編碼格式:UTF-8,系統(tǒng)Mac OS X版本:10.15.6
INFO->
~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~ JSON ~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~
> {
> ① . "pages":[
> ② . . . "C",
> ② . . . "D"
> ① . ],
> ① . "author":"J. R. R. Tolkien",
> ① . "price":22.99,
> ① . "isbn":"0-395-19395-8",
> ① . "page":"C",
> ① . "category":"fiction",
> ① . "title":"The Lord of the Rings"
> }
~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~ JSON ~?~~?~~?~~?~~?~~?~~?~~?~~?~~?~
Process finished with exit code 0
為空判斷
事實證明這也是一個坑,不過可以使用size 0
這個API
替換一下。
- 公眾號FunTester首發(fā),更多原創(chuàng)文章:450+原創(chuàng)文章,歡迎關(guān)注、交流,禁止第三方擅自轉(zhuǎn)載。