jsonpath语法大全_jsonpath使用详解

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(2172)   2023-03-28 11:29:14

json-path 组件使用

java 版 jsonpath
java 版 jsonpath

引入依赖

        <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] 支持数组切片语法

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

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

  • (<expr>) 使用表达式

  • ?(<boolean expr>)进行数据筛选

示例

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

 

 

与xPath对比

XPath JsonPath Result
/store/book/author $.store.book[*].author 所有book的author节点
//author $..author 所有author节点
/store/* $.store.* store下的所有节点,book数组和bicycle节点
/store//price $.store..price store下的所有price节点
//book[3] $..book[2] 匹配第3个book节点
//book[last()] $..book[(@.length-1)],或 $..book[-1:] 匹配倒数第1个book节点
//book[position()<3] $..book[0,1],或 $..book[:2] 匹配前两个book节点
//book[isbn] $..book[?(@.isbn)] 过滤含isbn字段的节点
//book[price<10] $..book[?(@.price<10)] 过滤price<10的节点
//* $..* 递归匹配所有子节点

提示:
1.返回对象,默认是 LinkedHashMap ;一般情况不会自动转换为具体对象;
2.返回集合,默认是JSONArray

地址:https://www.leftso.com/article/796.html

相关阅读

json-path 组件使用java 版 jsonpath引入依赖        &lt;dependency&gtl;            &lt;groupId&gtl;com.jayw...
Java Queue队列使用入门详解
java8 Function函数编程详解Function函数基础定义和使用 public static void t1(){ Function&lt;Integer,Int...
Supplier详解源码分析:package java.util.function; @FunctionalInterface public interface Supplier&lt;T&...
AtomicInteger学习指南,AtomicInteger使用教程。Java编程 AtomicInteger使用详解
okhttp使用详解,okhttp入门教程,okhttp应用,其实在Java web中okhttp使用还是不及Apache httpclient的。但是由于安卓中抛弃了httpclient,所以...
httpclient4.5使用详解 httpclient 4.5 post传递json参数
[JAVA]_图文详解CKeditor4.4.X版本添加程序代码高亮插件codesnippet,ckeditor
Apache HttpClient 4.x 使用详解