FunTester JsonPath 实践 (三)

FunTester · 2020年08月17日 · 755 次阅读

书接上文和上上文:

本期讲一下获取数组时增加过滤条件,这里用到的语法稍微复杂一点点。主要的过滤条件有几类:属性是否存在属性值比较属性值与属性值数组长度求值

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[?(@.isbn)]

代码:

Object read = JsonPath.read(json, "$..book[?(@.isbn)]");
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":"Herman Melville",
>  ① . "price":8.99,
>  ① . "isbn":"0-553-21311-3",
>  ① . "category":"fiction",
>  ① . "title":"Moby Dick"}
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
INFO-> 
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
>  {
>  ① . "author":"J. R. R. Tolkien",
>  ① . "price":22.99,
>  ① . "isbn":"0-395-19395-8",
>  ① . "category":"fiction",
>  ① . "title":"The Lord of the Rings"}
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~

Process finished with exit code 0

  • 暂时没有找到提供验证属性不存在的API,不过这个可以通过另外的方式实现,例如:属性值比较属性值正则匹配等等,后面会讲到。

属性值比较

字符串比较:

jsonpath$..book[?(@.isbn == '0-395-19395-8')]

数值比较:

jsonpath$..book[?(@.price > 20)]

  • 这里语法支持不同数据类型的自动化转换的,跟其他脚本语言一样。JSonpath还支持更多的值标胶写法,这个以后单独写篇文章讲一讲。

代码:

Object read = JsonPath.read(json, "$..book[?(@.price > 20)]");
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":"J. R. R. Tolkien",
>  ① . "price":22.99,
>  ① . "isbn":"0-395-19395-8",
>  ① . "category":"fiction",
>  ① . "title":"The Lord of the Rings"}
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~

Process finished with exit code 0

属性值与属性值

这个比较简单,涉及到一个JSonpath语法的嵌套问题。

jsonpath$..book[?(@.price > $['expensive'])]

  • 这里语法的嵌套基本是个套娃,不过个人还是不建议使用套娃,毕竟标记语法当然是越简单越好,非常强调可读性。

代码:

Object read = JsonPath.read(json, "$..book[?(@.price > $['expensive'])]");
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":"Evelyn Waugh",
>  ① . "price":12.99,
>  ① . "category":"fiction",
>  ① . "title":"Sword of Honour"}
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
INFO-> 
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
>  {
>  ① . "author":"J. R. R. Tolkien",
>  ① . "price":22.99,
>  ① . "isbn":"0-395-19395-8",
>  ① . "category":"fiction",
>  ① . "title":"The Lord of the Rings"}
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~

Process finished with exit code 0

数组长度求值

这个就更简单了,求数组长度的一个API

jsonpath$..book.length()

  • 这里有一个,如果把length()方法用到对数组过滤或者指定数组对象之后,会变成求该对象属性个数或者过滤后数组的长度的功能了,返回结果是个数值类型的数组。

代码:

Object read = JsonPath.read(json, "$..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


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

热文精选

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