• JsonPath实践(六)


    书接上文和上上文:

    之前分享了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-> 当前用户: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()

    代码:

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

    等效写法继续省略……

    控制台输出:

    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()

    代码:

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

    等效写法继续省略……

    控制台输出:

    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()

    代码:

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

    等效写法继续省略……

    控制台输出:

    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()

    代码:

            Object read = JsonPath.read(json, "$.ss.length()");
            output(read);
    
            Object read = JsonPath.read(json, "$.store.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
    
    

    求数组的和

    jsonpath$.ss.sum()

    代码:

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

    等效写法继续省略……

    控制台输出:

    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的内容,使用GroovyGroovy重载操作符(终极版)功能,敬请期待。


    • 公众号FunTester首发,更多原创文章:450+原创文章,欢迎关注、交流,禁止第三方擅自转载。

    热文精选

  • 相关阅读:
    彻底理解JavaScript原型
    Spring mvc中@RequestMapping 6个基本用法小结
    window.onload、DOMContentLoaded和$(document).ready()
    Cookie/Session机制详解
    微信公众平台开发接口PHP SDK完整版
    Js获取当前浏览器的高和宽度
    ExtJS4学习--多表头Grid
    get传输时,会将加号+ 转换为空格
    svg实现 圆形 点击扩大、消失
    SVG 总结
  • 原文地址:https://www.cnblogs.com/FunTester/p/13575934.html
Copyright © 2020-2023  润新知