FunTester JsonPath 实践 (四)

FunTester · 2020年08月20日 · 837 次阅读

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

本期继续将如何处理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+ 原创文章,欢迎关注、交流,禁止第三方擅自转载。

热文精选

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册