JSONPath的简单使用

jsonPath的简单使用

  • 前言
  • 一、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有一套语法,更多姿势前往官网看看语法。

你可能感兴趣的:(json,java,开发语言)