JsonPath实践(六)

2020-09-01 15:23:33 浏览数 (1)

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

json数据

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

代码语言:javascript复制
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()

代码:

代码语言:javascript复制
        Object read = JsonPath.read(json, "$.ss.min()");
        output(read);

等效写法继续省略……

控制台输出:

代码语言:javascript复制
INFO-> 当前用户:fv,IP:10.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

  • 这里需要注意,该方法返回值是一个double的数值,测试中用的int整型,但是结果返回的是4.0

获取数组的最大值

jsonpath$.ss.max()

代码:

代码语言:javascript复制
        Object read = JsonPath.read(json, "$.ss.max()");
        output(read);

等效写法继续省略……

控制台输出:

代码语言:javascript复制
INFO-> 当前用户:fv,IP:10.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

  • 同样的,max函数返回的也是double浮点型数据。

获取数组的平均值

jsonpath$.ss.avg()

代码:

代码语言:javascript复制
        Object read = JsonPath.read(json, "$.ss.avg()");
        output(read);

等效写法继续省略……

控制台输出:

代码语言:javascript复制
INFO-> 当前用户:fv,IP:10.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
  • 同样的,max函数返回的也是double浮点型数据。

获取数组的标准差

jsonpath$.ss.stddev()

代码:

代码语言:javascript复制
        Object read = JsonPath.read(json, "$.ss.stddev()");
        output(read);

等效写法继续省略……

控制台输出:

代码语言:javascript复制
INFO-> 当前用户:fv,IP:10.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

  • 同样的,stddev函数返回的也是double浮点型数据。

获取数组的长度

  • 对于json数组适用。

jsonpath$.ss.length()

jsonpath$.store.book.length()

代码:

代码语言:javascript复制
        Object read = JsonPath.read(json, "$.ss.length()");
        output(read);
代码语言:javascript复制
        Object read = JsonPath.read(json, "$.store.book.length()");
        output(read);

等效写法继续省略……

控制台输出:

代码语言:javascript复制
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

求数组的和

jsonpath$.ss.sum()

代码:

代码语言:javascript复制
        Object read = JsonPath.read(json, "$.ss.sum()");
        output(read);

等效写法继续省略……

控制台输出:

代码语言:javascript复制
INFO-> 当前用户:fv,IP:10.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

  • 同样的,sun函数返回的也是double浮点型数据。

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

0 人点赞