JSONPath的简单使用


前言

需求:收到一个不确定的对象,根据配置取到需要的内容。
例子1:得到一个对象(是一个List),需要集合内每条item的某个属性,组成一个新的集合。
例子2:得到一个对象(内嵌List),需要对象内,指定集合里,(包含某个属性的)item的某个属性,组成新的集合
例子3:得到一个对象,获取对象指定的属性

JSONPath的Git地址:https://github.com/json-path/JsonPath
JSONPath的GitCode地址(没有翻墙的看这个):https://gitcode.net/mirrors/json-path/jsonpath?utm_source=csdn_github_accelerator


一、JSONPath依赖

        <!--jsonPath-->
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>2.7.0</version>
        </dependency>

二、使用步骤


import com.alibaba.fastjson.JSONObject;
import com.jayway.jsonpath.JsonPath;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class JsonPathDemo {

    public static JSONObject getJsonObject() {
        InputStream defaultColumnInputStream;
        JSONObject json = null;
        try {
            ClassPathResource defaultData = new ClassPathResource("static/jsonPath.json");
            defaultColumnInputStream = defaultData.getInputStream();
            json = JSONObject.parseObject(defaultColumnInputStream, JSONObject.class);
            System.out.println(json);
            System.out.println("--------------------");
        } catch (IOException e) {
            System.out.println("异常了");
        }
        return json;
    }


    public static void main(String[] args) {
        JSONObject json = getJsonObject();
        List<String> authors = JsonPath.read(json, "$.store.book[*].author");
        System.out.println(authors.toString());

        List<String> authors1 = JsonPath.read(json, "$..author");
        System.out.println(authors1.toString());

        List<String> authorsOfBooksWithISBN = JsonPath.parse(json).read("$.store.book[?(@.isbn)].author");
        System.out.println(authorsOfBooksWithISBN.toString());


        String dateJson = "{\"date_as_long\" : 1411455611975}";
        Date date = JsonPath.parse(dateJson).read("$['date_as_long']", Date.class);
        System.out.println(date);
    }
}

三、日志输出

14:11:59.424 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['store']['book'][*]['author']
["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
14:11:59.436 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $..['author']
["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
14:11:59.451 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['store']['book'][?]['author']
14:11:59.453 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: @['isbn']
14:11:59.466 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: @['isbn']
14:11:59.466 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: @['isbn']
14:11:59.467 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: @['isbn']
["Herman Melville","J. R. R. Tolkien"]
14:11:59.472 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['date_as_long']

总结

简单的JSONPath使用。
JSONPath有一套语法,更多姿势前往官网看看语法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值