动力节点首页 全国咨询热线:400-8080-105

绑定手机号,登录
手机号

验证码

微信登录
手机号登录
手机号

验证码

30天自动登录
微信登录与注册
微信扫码登录与注册

扫码关注微信公众号完成登录与注册
手机号登录
首页 > 文章

jsonpath语法大全

07-29 12:29 347浏览
举报 T字号
  • 大字
  • 中字
  • 小字

引入依赖

        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>2.4.0</version>
        </dependency>

语法说明

JsonPath语法要点:

$ 表示文档的根元素

@ 表示文档的当前元素

.node_name 或 ['node_name'] 匹配下级节点

[index] 检索数组中的元素

[start:end:step] 支持数组切片语法

* 作为通配符,匹配所有成员

.. 子递归通配符,匹配成员的所有子元素

() 使用表达式

?()进行数据筛选

示例

{
    "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
        }
    }
}

获取bicycle的price

JsonPath表达式:$.store.bicycle.price

String read = JsonPath.parse(jsonString).read("$.store.bicycle.price", String.class);
System.out.println(read);

结果:

19.95

所有book的author节点

JsonPath表达式:$.store.book[*].author

List<String> read = JsonPath.parse(jsonString).read("$.store.book[*].author", new TypeRef<List<String>>() {});

结果:

["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]

所有author节点

JsonPath表达式: $..author

List<JSONObject> read = JsonPath.parse(jsonString).read("`$..author`", new TypeRef<List<JSONObject>>() {});

结果:

          {
                "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
            }

store下的所有节点,book数组和bicycle节点

JsonPath表达式:$.store.*

List<JSONObject> read = JsonPath.parse(jsonString).read("$.store.*", new TypeRef<List<JSONObject>>() {});

结果:

[
    [
        {
            "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
        }
    ],
    {
        "color":"red",
        "price":19.95
    }
]

store下的所有price节点

JsonPath表达式:$.store..price

List<JSONObject> read = JsonPath.parse(jsonString).read("$.store..price", new TypeRef<List<JSONObject>>() {});

结果:

[8.95,12.99,8.99,22.99,19.95]

匹配第3个book节点

JsonPath表达式:$..book[2]

List<JSONObject> read = JsonPath.parse(jsonString).read("$..book[2]", new TypeRef<List<JSONObject>>() {});

结果:

[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]

匹配倒数第1个book节点

JsonPath表达式:$..book[-1:]

List<JSONObject> read = JsonPath.parse(jsonString).read("$..book[-1:]", new TypeRef<List<JSONObject>>() {});

结果:

[{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]

匹配前两个book节点

JsonPath表达式:$..book[0,1],或 $..book[:2]

List<JSONObject> read = JsonPath.parse(jsonString).read("$..book[0,1]", new TypeRef<List<JSONObject>>() {});

结果:

[{"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}]

book节点下过滤含isbn字段的节点

JsonPath表达式:$.store.book[*][?(@.isbn)] 不包含 $.store.book[*][?(!@.isbn)]

List<JSONObject> read = JsonPath.parse(jsonString).read("$.store.book[*][?(@.isbn)]", new TypeRef<List<JSONObject>>() {});

结果:

[
    {
        "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
    }
]

过滤book price<10的节点

JsonPath表达式:$..book[?(@.price<10)]

List<JSONObject> read = JsonPath.parse(jsonString).read("$..book[?(@.price<10)]", new TypeRef<List<JSONObject>>() {});

结果:

[
    {
        "category":"reference",
        "author":"Nigel Rees",
        "title":"Sayings of the Century",
        "price":8.95
    },
    {
        "category":"fiction",
        "author":"Herman Melville",
        "title":"Moby Dick",
        "isbn":"0-553-21311-3",
        "price":8.99
    }
]

动力节点在线课程涵盖零基础入门,高级进阶,在职提升三大主力内容,覆盖Java从入门到就业提升的全体系学习内容。全部Java视频教程免费观看,相关学习资料免费下载!对于火爆技术,每周一定时更新!如果想了解更多相关技术,可以到动力节点在线免费观看JSONPath视频教程哦!

0人推荐
共同学习,写下你的评论
0条评论
代码小兵498
程序员代码小兵498

153篇文章贡献528999字

相关课程 更多>

作者相关文章更多>

推荐相关文章更多>

Java初学者学习方法

代码小兵64503-29 11:46

两道经典算法问题

代码小兵51603-29 13:18

Java中模拟高并发的方法

代码小兵87208-06 11:36

Java程序员必备的一些流程图

代码小兵64503-29 15:27

Java接口限流的实现方法

代码小兵57603-29 17:52

发评论

举报

0/150

取消