zoukankan      html  css  js  c++  java
  • JsonPath实践(四)

    书接上文和上上文以及上上上文:

    本期继续将如何处理json数组,主要内容是通过正则过滤json数组中的数据,以及通过正则校验json节点值。

    JSonpath中的正则语法是通用的,但是使用方法跟Groovy非常类似。有兴趣的同学参考:Java和Groovy正则使用。使用=~这个标记语法表示正则匹配,然后用前后两个/符号表示正则的内容,这一点跟Groovy一模一样,还有多了一种忽略大小写的语法,就是在正则语句后面的/加上i这个字母(暂时没发现其他字母的标记功能)。

    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[?(@.author =~ /.*Rees/)]

    或者使用路径表示:

    jsonpath$.store.book[?(@.author =~ /.*Rees/)]

    • 这里表示倒数第一个对象

    代码:

            Object read = JsonPath.read(json, "$.store.book[?(@.author =~ /.*Rees/)]");
            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":"Nigel Rees",
    >  ① . "price":8.95,
    >  ① . "category":"reference",
    >  ① . "title":"Sayings of the Century"
    >  }
    ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
    
    Process finished with exit code 0
    
    

    忽略大小写

    jsonpath$..book[?(@.author =~ /.*REES/)]

    或者使用忽略大小写语法:

    jsonpath$.store.book[?(@.author =~ /.*REES/i)]

    • 这里表示倒数第一个对象

    代码:

            Object read = JsonPath.read(json, "$.store.book[?(@.author =~ /.*REES/)]");
            output(JSONArray.parseArray(read.toString()).isEmpty());
    
            Object read = JsonPath.read(json, "$.store.book[?(@.author =~ /.*Rees/i)]");
            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-> true
    
    Process finished with exit code 0
    
    
    INFO-> 当前用户:fv,IP:10.60.192.21,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
    INFO-> 
    ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
    >  {
    >  ① . "author":"Nigel Rees",
    >  ① . "price":8.95,
    >  ① . "category":"reference",
    >  ① . "title":"Sayings of the Century"
    >  }
    ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
    
    Process finished with exit code 0
    
    

    正则验证节点值

    • 这个不支持,哈哈,我会封装一下,让它支持。

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

    热文精选

  • 相关阅读:
    Base64编码与图片互转
    YUI Compressor–另一款压缩JS/CSS的小工具
    布局和展现相关的建议──给用户体验设计者
    实现左右边框的最低碳代码~~
    三角的一个特殊做法(一个奇技淫巧~)
    数组指针和指针数组
    vs2008建win32基本项目
    vs2008快捷键
    (转)HTML特殊字符
    (转)html5 Placeholder属性兼容IE6、7方法
  • 原文地址:https://www.cnblogs.com/FunTester/p/13533303.html
Copyright © 2011-2022 走看看