JSONPath的介绍:

JsonPath 是一种简单的方法来提取给定JSON文档的部分内容。
JsonPath表达式总是以与XPath表达式结合使用XML文档相同的方式引用JSON结构。
JsonPath中的“根成员对象”始终称为$,无论是对象还是数组。

JsonPath 对应的maven包

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

操作符:说明JSONPath语法元素和对应XPath元素的对比

函数

函数可以在路径的尾部调用,函数的输出是路径表达式的输出,该函数的输出是由函数本身所决定的。

函数 描述 输出
min() 提供数字数组的最小值 Double
max() 提供数字数组的最大值 Double
avg() 提供数字数组的平均值 Double
stddev() 提供数字数组的标准偏差值 Double
length() 提供数组的长度 Integer

过滤器运算符

过滤器是用于筛选数组的逻辑表达式。一个典型的过滤器将是[?(@.age > 18)],其中@表示正在处理的当前项目。 可以使用逻辑运算符&&和||创建更复杂的过滤器。 字符串文字必须用单引号或双引号括起来([?(@.color == ‘blue’)] 或者 [?(@.color == “blue”)]).

操作符 描述
== left等于right(注意1不等于’1’)
!= 不等于
< 小于
<= 小于等于
> 大于
>= 大于等于
=~ 匹配正则表达式[?(@.name =~ /foo.*?/i)]
in 左边存在于右边 [?(@.size in [‘S’, ‘M’])]
nin 左边不存在于右边
size (数组或字符串)长度
empty (数组或字符串)为空

Java操作实例

{"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}},"expensive": 10
}
编号 JsonPath 结果
1 $.store.book[*].author 获取json中store下book下的所有author值
2 $…author 获取所有json中所有author的值
3 $.store.* 所有的东西,书籍和自行车
4 $.store…price 获取json中store下所有price的值
5 $…book[2] 获取json中book数组的第3个值
6 $…book[-2] 倒数的第二本书
7 $…book[0,1] 前两本书
8 $…book[:2] 从索引0(包括)到索引2(排除)的所有图书
9 $…book[1:2] 从索引1(包括)到索引2(排除)的所有图书
10 $…book[-2:] 获取json中book数组的最后两个值
11 $…book[2:] 获取json中book数组的第3个到最后一个的区间值
12 $…book[?(@.isbn)] 获取json中book数组中包含isbn的所有值
13 $.store.book[?(@.price < 10)] 获取json中book数组中price<10的所有值
14 $…book[?(@.price <= $[‘expensive’])] 获取json中book数组中price<=expensive的所有值
15 $…book[?(@.author =~ /.*REES/i)] 获取json中book数组中的作者以REES结尾的所有值(REES不区分大小写)
16 $…* 逐层列出json中的所有值,层级由外到内
17 $…book.length() 获取json中book数组的长度

以上的三个点都是两个点

结果:

编号1:

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

编号2:

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

编号3:

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

编号4:

[8.95,12.99,8.99,22.99,19.95
]

编号5:

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

编号6:

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

编号7:

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

编号8:

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

编号9:

[{"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99}
]

编号10:

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

编号11:

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

编号12:

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

编号13:

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

编号14:

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

编号15:

[{"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}
]

编号16:

[{"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}},10,[{"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},{"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},"reference","Nigel Rees","Sayings of the Century",8.95,"fiction","Evelyn Waugh","Sword of Honour",12.99,"fiction","Herman Melville","Moby Dick","0-553-21311-3",8.99,"fiction","J. R. R. Tolkien","The Lord of the Rings","0-395-19395-8",22.99,"red",19.95
]

编号17:

2

json字符串的读取

方法一:解析一次json

String json = "...";List<String> authors = JsonPath.read(json, "$.store.book[*].author");

方法二:对同一个json解析多次,可以使用ReadContext、WriteContext

String json = "...";ReadContext ctx = JsonPath.parse(json);List<String> authorsOfBooksWithISBN = ctx.read("$.store.book[?(@.isbn)].author");List<Map<String, Object>> expensiveBooks = JsonPath.using(configuration).parse(json).read("$.store.book[?(@.price > 10)]", List.class);

返回类型

1.通常read后的返回值会进行自动转型到指定的类型,对应明确定义definite的表达式,应指定其对应的类型。

// 抛出 java.lang.ClassCastException 异常
List<String> list = JsonPath.parse(json).read("$.store.book[0].author")// 正常
String author = JsonPath.parse(json).read("$.store.book[0].author")

2.默认情况下,MappingProvider SPI提供了一个简单的对象映射器。 这允许您指定所需的返回类型,MappingProvider将尝试执行映射。 在下面的示例中,演示了Long和Date之间的映射。

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

3.如果您将JsonPath配置为使用JacksonMappingProvider或GsonMappingProvider,您甚至可以将JsonPath输出直接映射到POJO中。

Book book = JsonPath.parse(json).read("$.store.book[0]", Book.class);

欢迎光临我的公众号,一起学习:程序媛米佳

使用 JSONPath 解析 JSON内容 详细相关推荐

  1. 使用jsonpath解析json内容

    JsonPath提供的json解析非常强大,它提供了类似正则表达式的语法,基本上可以满足所有你想要获得的json内容.下面我把官网介绍的每个表达式用代码实现,可以更直观的知道该怎么用它. 一.首先需要 ...

  2. JSONPath 解析 JSON 内容详解(翻译自 github)

    Github :https://github.com/json-path/JsonPath http://www.ibloger.net/article/2329.html JSONPath Onli ...

  3. JSONPath 解析 JSON 内容详解(自 github)

    Github :https://github.com/json-path/JsonPath http://www.ibloger.net/article/2329.html JSONPath Onli ...

  4. java json path_Java使用JSONPath解析JSON完整内容详解

    JsonPath是一种简单的方法来提取给定JSON文档的部分内容. JsonPath有许多编程语言,如Javascript,Python和PHP,Java. JsonPath提供的json解析非常强大 ...

  5. 使用 JSONPath 解析 JSON 完整内容详解

    JsonPath是一种简单的方法来提取给定JSON文档的部分内容. JsonPath有许多编程语言,如Javascript,Python和PHP,Java. JsonPath提供的json解析非常强大 ...

  6. Java整合Jsonpath解析Json字符串

    JsonPath是一种简单的方法来提取给定JSON文档的部分内容 引入依赖 <dependency><groupId>com.jayway.jsonpath</group ...

  7. jsonpath - 使用 JSONPath 解析 JSON

    JsonPath是一种简单的方法来提取给定JSON文档的部分内容. JsonPath提供的json解析非常强大,它提供了类似正则表达式的语法,基本上可以满足所有你想要获得的json内容. JsonPa ...

  8. C#深入解析Json格式内容

    继上一篇<浅谈C#手动解析Json格式内容>我又来分析加入了一些功能让 这个解析类更实用 本章节最会开放我最终制作成功的Anonymous.Json.dll这个解析库 需要的拿走~ 功能继 ...

  9. JSONPath(XPath for JSON)解析 JSON教程

    1. 介绍 类似于XPath在xml文档中的定位,JsonPath表达式通常是用来路径检索或设置Json的.其表达式可以接受"dot–notation"和"bracket ...

最新文章

  1. python【力扣LeetCode算法题库】100-相同的树
  2. java开关用法_如何在Java中使用带开关盒的枚举?
  3. GDCM:提取DICOM文件数据元素值字段的测试程序
  4. 《团队激励与沟通》第 7 讲——团队合作概述 重点部分总结
  5. 详细解释下头条图文和微头条发布建议
  6. ISA Server 2004软件防火墙相关配置
  7. Lync和Exchange 2013集成PART1:准备所需证书
  8. 金色圣诞幻灯片AE模板
  9. PHP中的错误处理set_error_handler()与trigger_error()的问题
  10. 设计模式-关于模式的一些很基本的知识点
  11. oracle存储过程实例
  12. 【转载】士兵突击 经典语录
  13. SpringBoot巧用静态内部类优雅地接收参数
  14. 学术论文检索--搜索引擎篇
  15. ORACLE介质管理库MML
  16. (一)云计算概念了解
  17. CANoe-第3个仿真工程-总线仿真-1概述
  18. 幼儿园stem教育的发展有什么意义
  19. 奶爸日记-好好弹钢琴的保证书
  20. 根据html改为ftl模板生成pdf文件,支持中文及换行

热门文章

  1. Java-互联网通信流程介绍【基础】
  2. 第三方支付创新与风控是未来关键——拉卡拉支付
  3. 大话西游手游有双系统服务器吗,大话西游手游有几个版本_大话西游手游官服和混服怎么区分_玩游戏网...
  4. 人脸检测算法落地详解
  5. python计算长方形的周长和面积_计算矩形的周长和面积
  6. 中国文化概论名词解释题
  7. 安装软件出错--The requested URL returned error: 404
  8. 职业经验 聊一聊职业发展
  9. 何恺明:从高考状元到CV领域年轻翘楚,靠“去雾算法”成为“CVPR最佳论文”首位华人得主...
  10. vivos机器人_【vivoNEXS评测】操作:人工智能无处不在 Jovi AI正在接管系统-中关村在线...