书接上文和上上文:

之前分享了jsonpath的部分API使用,基本已经把基础的内容讲完了,今天分享一下JsonPath API中的函数的使用方法,其实之前讲到的一些json数组的过滤中已经用到了一些函数,大概是因为功能不一样吧,这里将的函数都是处理json数组的,而不是过滤数组的条件。

json 数据

在原来的数据基础上增加了部分字段和部分节点。

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]" +
                "}");

获取数组最小值

jsonpath$.ss.min()

代码:

Object read = JsonPath.read(json, "$.ss.min()");
output(read);

等效写法继续省略……

控制台输出:

INFO-> 当前用户fvIP10.60.192.21工作目录/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
INFO-> 4.0

Process finished with exit code 0

获取数组的最大值

jsonpath$.ss.max()

代码:

Object read = JsonPath.read(json, "$.ss.max()");
output(read);

等效写法继续省略……

控制台输出:

INFO-> 当前用户fvIP10.60.192.21工作目录/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
INFO-> 32.0

Process finished with exit code 0

获取数组的平均值

jsonpath$.ss.avg()

代码:

Object read = JsonPath.read(json, "$.ss.avg()");
output(read);

等效写法继续省略……

控制台输出:

INFO-> 当前用户fvIP10.60.192.21工作目录/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
INFO-> 22.75

Process finished with exit code 0

获取数组的标准差

jsonpath$.ss.stddev()

代码:

Object read = JsonPath.read(json, "$.ss.stddev()");
output(read);

等效写法继续省略……

控制台输出:

INFO-> 当前用户fvIP10.60.192.21工作目录/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
INFO-> 11.431863365173676

Process finished with exit code 0

获取数组的长度

jsonpath$.ss.length()

jsonpath$.store.book.length()

代码:

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

等效写法继续省略……

控制台输出:

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

Process finished with exit code 0

求数组的和

jsonpath$.ss.sum()

代码:

Object read = JsonPath.read(json, "$.ss.sum()");
output(read);

等效写法继续省略……

控制台输出:

INFO-> 当前用户fvIP10.60.192.21工作目录/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
INFO-> 91.0

Process finished with exit code 0

自此,JsonPath API系列已经更完了,我在积极准备JsonPath util的内容,使用GroovyGroovy 重载操作符(终极版)功能,敬请期待。


热文精选


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