JsonPath 过滤操作符示例

前面介绍了 JsonPath 提供的过滤操作符,下面将给出一些示例,供读者参考。

比较操作符

下面示例演示 >(大于)、>=(大于等于)、<(小于)、<=(小于等于)、==(等于)和 !=(不等于)比较操作符的用法,代码如下:

package com.hxstrive.json_path.filter;

import com.jayway.jsonpath.JsonPath;

/**
 * >、>=、<、<=、==、!= 过滤器
 * @author hxstrive.com
 */
public class FilterDemo02 {

    public static void main(String[] args) {
        String json = "{" +
                "    \"store\": {" +
                "        \"book\": [" +
                "            {" +
                "                \"title\": \"Sayings of the Century\"," +
                "                \"price\": 8.95" +
                "            }," +
                "            {" +
                "                \"title\": \"Sword of Honour\"," +
                "                \"price\": 12.99" +
                "            }," +
                "            {" +
                "                \"title\": \"Moby Dick\"," +
                "                \"price\": 8.99" +
                "            }," +
                "            {" +
                "                \"title\": \"The Lord of the Rings\"," +
                "                \"price\": 22.99" +
                "            }" +
                "        ]" +
                "    }," +
                "    \"expensive\": 10" +
                "}";

        // [{"title":"Sayings of the Century","price":8.95}]
        Object obj = JsonPath.read(json, "$.store.book[*][?(@.price < 8.99)]");
        System.out.println(obj);

        // [{"title":"Sayings of the Century","price":8.95},{"title":"Moby Dick","price":8.99}]
        obj = JsonPath.read(json, "$.store.book[*][?(@.price <= 8.99)]");
        System.out.println(obj);

        // [{"title":"The Lord of the Rings","price":22.99}]
        obj = JsonPath.read(json, "$.store.book[*][?(@.price > 12.99)]");
        System.out.println(obj);

        // [{"title":"Sword of Honour","price":12.99},{"title":"The Lord of the Rings","price":22.99}]
        obj = JsonPath.read(json, "$.store.book[*][?(@.price >= 12.99)]");
        System.out.println(obj);

        // [{"title":"Sword of Honour","price":12.99}]
        obj = JsonPath.read(json, "$.store.book[*][?(@.price == 12.99)]");
        System.out.println(obj);

        // [{"title":"Sayings of the Century","price":8.95},{"title":"Moby Dick","price":8.99},{"title":"The Lord of the Rings","price":22.99}]
        obj = JsonPath.read(json, "$.store.book[*][?(@.price != 12.99)]");
        System.out.println(obj);
    }

}

in 和 nin 操作符

下面示例演示 in(判断值在指定的集合中)和 nin(判断值不在指定的集合中)操作符,代码如下:

package com.hxstrive.json_path.filter;

import com.jayway.jsonpath.JsonPath;

/**
 * in、nin 过滤器
 * @author hxstrive.com
 */
public class FilterDemo03 {

    public static void main(String[] args) {
        String json = "{" +
                "    \"store\": {" +
                "        \"book\": [" +
                "            {" +
                "                \"title\": \"Java\"," +
                "                \"price\": 8.95" +
                "            }," +
                "            {" +
                "                \"title\": \"C#\"," +
                "                \"price\": 12.99" +
                "            }," +
                "            {" +
                "                \"title\": \"C++\"," +
                "                \"price\": 8.99" +
                "            }," +
                "            {" +
                "                \"title\": \"PHP\"," +
                "                \"price\": 22.99" +
                "            }" +
                "        ]" +
                "    }," +
                "    \"expensive\": 10" +
                "}";

        // [{"title":"Java","price":8.95},{"title":"C#","price":12.99}]
        Object obj = JsonPath.read(json, "$.store.book[*][?(@.title in ['Java', 'C#'])]");
        System.out.println(obj);

        // [{"title":"C++","price":8.99},{"title":"PHP","price":22.99}]
        obj = JsonPath.read(json, "$.store.book[*][?(@.title nin ['Java', 'C#'])]");
        System.out.println(obj);
    }

}

集合操作符

下面示例演示集合的子集、交集操作符,代码如下:

package com.hxstrive.json_path.filter;

import com.jayway.jsonpath.JsonPath;

/**
 * subsetof、anyof、noneof 过滤器
 * @author hxstrive.com
 */
public class FilterDemo04 {

    public static void main(String[] args) {
        String json = "{" +
                "    \"store\": {" +
                "        \"book\": [" +
                "            {" +
                "                \"interests\": [\"足球\",\"登山\"]," +
                "                \"username\": \"Tom\"" +
                "            }," +
                "            {" +
                "                \"interests\": [\"篮球\",\"跑步\"]," +
                "                \"username\": \"Helen\"" +
                "            }," +
                "            {" +
                "                \"interests\": [\"篮球\",\"游泳\"]," +
                "                \"username\": \"Bill\"" +
                "            }," +
                "            {" +
                "                \"interests\": [\"攀岩\",\"跳伞\"]," +
                "                \"username\": \"Any\"" +
                "            }" +
                "        ]" +
                "    }," +
                "    \"expensive\": 10" +
                "}";

        // [{"interests":["篮球","跑步"],"username":"Helen"}]
        Object obj = JsonPath.read(json, "$.store.book[*][?(@.interests subsetof ['篮球', '跑步'])]");
        System.out.println(obj);

        // [{"interests":["篮球","跑步"],"username":"Helen"},{"interests":["篮球","游泳"],"username":"Bill"}]
        obj = JsonPath.read(json, "$.store.book[*][?(@.interests anyof ['篮球', '跑步'])]");
        System.out.println(obj);

        // [{"interests":["足球","登山"],"username":"Tom"},{"interests":["攀岩","跳伞"],"username":"Any"}]
        obj = JsonPath.read(json, "$.store.book[*][?(@.interests noneof ['篮球', '跑步'])]");
        System.out.println(obj);
    }

}

size 操作符

下面示例演示 size 操作符,判断数组的长度,代码如下:

package com.hxstrive.json_path.filter;

import com.jayway.jsonpath.JsonPath;

/**
 * size 过滤器
 * @author hxstrive.com
 */
public class FilterDemo01 {

    public static void main(String[] args) {
        String json = "{" +
                "    \"store\": {" +
                "        \"book\": [" +
                "            [1,2,3,4]," +
                "            [1,2,3]," +
                "            [1,2]," +
                "            [5,6,7]" +
                "        ]," +
                "        \"bicycle\": {" +
                "            \"color\": \"red\"," +
                "            \"price\": 19.95" +
                "        }" +
                "    }," +
                "    \"expensive\": 10" +
                "}";

        // 匹配成功,返回 [[1,2,3],[5,6,7]]
        Object obj = JsonPath.read(json, "$.store[*][?(@ size 3)]");
        System.out.println(obj);

        // 匹配成功,返回 [[1,2,3,4]]
        obj = JsonPath.read(json, "$.store.book[?(@ size 4)]");
        System.out.println(obj);
    }

}


说说我的看法
全部评论(
没有评论
关于
本网站专注于 Java、数据库(MySQL、Oracle)、Linux、软件架构及大数据等多领域技术知识分享。涵盖丰富的原创与精选技术文章,助力技术传播与交流。无论是技术新手渴望入门,还是资深开发者寻求进阶,这里都能为您提供深度见解与实用经验,让复杂编码变得轻松易懂,携手共赴技术提升新高度。如有侵权,请来信告知:hxstrive@outlook.com
公众号