书接上文和上上文:

本期讲一下获取数组时增加过滤条件,这里用到的语法稍微复杂一点点。主要的过滤条件有几类:属性是否存在属性值比较属性值与属性值数组长度求值

json 数据

首先看官方给的json数据的Demo(我做了一点点修改):

JSONObject json = JSON.parseObject("{" +
                "    \"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" +
                "        }" +
                "    }," +
                "    \"expensive\": 10," +
                "    \"ss\": [32,32,4,23]" +
                "}");

验证属性是否存在

jsonpath$..book[?(@.isbn)]

代码:

Object read = JsonPath.read(json, "$..book[?(@.isbn)]");
output(JSONArray.parseArray(read.toString()));

等效写法省略……

控制台输出:

INFO-> 当前用户:fv,IP:10.60.192.21,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
INFO-> 
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
>  {
>  ① . "author":"Herman Melville",
>  ① . "price":8.99,
>  ① . "isbn":"0-553-21311-3",
>  ① . "category":"fiction",
>  ① . "title":"Moby Dick"}
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
INFO-> 
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
>  {
>  ① . "author":"J. R. R. Tolkien",
>  ① . "price":22.99,
>  ① . "isbn":"0-395-19395-8",
>  ① . "category":"fiction",
>  ① . "title":"The Lord of the Rings"}
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~

Process finished with exit code 0

属性值比较

字符串比较:

jsonpath$..book[?(@.isbn == '0-395-19395-8')]

数值比较:

jsonpath$..book[?(@.price > 20)]

代码:

Object read = JsonPath.read(json, "$..book[?(@.price > 20)]");
output(JSONArray.parseArray(read.toString()));

等效写法继续省略……

控制台输出:

INFO-> 当前用户:fv,IP:10.60.192.21,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
INFO-> 
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
>  {
>  ① . "author":"J. R. R. Tolkien",
>  ① . "price":22.99,
>  ① . "isbn":"0-395-19395-8",
>  ① . "category":"fiction",
>  ① . "title":"The Lord of the Rings"}
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~

Process finished with exit code 0

属性值与属性值

这个比较简单,涉及到一个JSonpath语法的嵌套问题。

jsonpath$..book[?(@.price > $['expensive'])]

代码:

Object read = JsonPath.read(json, "$..book[?(@.price > $['expensive'])]");
output(JSONArray.parseArray(read.toString()));

等效写法继续省略……

控制台输出:


INFO-> 当前用户:fv,IP:10.60.192.21,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
INFO-> 
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
>  {
>  ① . "author":"Evelyn Waugh",
>  ① . "price":12.99,
>  ① . "category":"fiction",
>  ① . "title":"Sword of Honour"}
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
INFO-> 
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
>  {
>  ① . "author":"J. R. R. Tolkien",
>  ① . "price":22.99,
>  ① . "isbn":"0-395-19395-8",
>  ① . "category":"fiction",
>  ① . "title":"The Lord of the Rings"}
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~

Process finished with exit code 0

数组长度求值

这个就更简单了,求数组长度的一个API

jsonpath$..book.length()

代码:

Object read = JsonPath.read(json, "$..book.length()");
output(read);

等效写法继续省略……

控制台输出:


INFO-> 当前用户:fv,IP:10.60.192.21,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
INFO-> [4]

Process finished with exit code 0


热文精选


↙↙↙阅读原文可查看相关链接,并与作者交流