JSONPath的简单使用

妖狐艹你老母 2024-03-30 14:30 141阅读 0赞

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依赖

  1. <!--jsonPath-->
  2. <dependency>
  3. <groupId>com.jayway.jsonpath</groupId>
  4. <artifactId>json-path</artifactId>
  5. <version>2.7.0</version>
  6. </dependency>

二、使用步骤

  1. import com.alibaba.fastjson.JSONObject;
  2. import com.jayway.jsonpath.JsonPath;
  3. import org.springframework.core.io.ClassPathResource;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.Date;
  7. import java.util.List;
  8. public class JsonPathDemo {
  9. public static JSONObject getJsonObject() {
  10. InputStream defaultColumnInputStream;
  11. JSONObject json = null;
  12. try {
  13. ClassPathResource defaultData = new ClassPathResource("static/jsonPath.json");
  14. defaultColumnInputStream = defaultData.getInputStream();
  15. json = JSONObject.parseObject(defaultColumnInputStream, JSONObject.class);
  16. System.out.println(json);
  17. System.out.println("--------------------");
  18. } catch (IOException e) {
  19. System.out.println("异常了");
  20. }
  21. return json;
  22. }
  23. public static void main(String[] args) {
  24. JSONObject json = getJsonObject();
  25. List<String> authors = JsonPath.read(json, "$.store.book[*].author");
  26. System.out.println(authors.toString());
  27. List<String> authors1 = JsonPath.read(json, "$..author");
  28. System.out.println(authors1.toString());
  29. List<String> authorsOfBooksWithISBN = JsonPath.parse(json).read("$.store.book[?(@.isbn)].author");
  30. System.out.println(authorsOfBooksWithISBN.toString());
  31. String dateJson = "{\"date_as_long\" : 1411455611975}";
  32. Date date = JsonPath.parse(dateJson).read("$['date_as_long']", Date.class);
  33. System.out.println(date);
  34. }
  35. }

三、日志输出

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

总结

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

发表评论

表情:
评论列表 (有 0 条评论,141人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JsonPath 运算符使用

    背景 因为之前用到Jmeter的Json 提取器涉及到JsonPath的使用,因此查找了一些文章,将里面的精华部分运算符的使用提取出来记录以下,方便自己以后使用。 J