项目特点

GitHub项目地址:https://github.com/json-path/JsonPath

主要功能:

  1. 将Json字符串转为Java Map对象(这个不算什么,FastJson之类的工具都可以)
  2. 通过强大的规则表达式定位字段,返回字段值或值集合(很厉害)

支持的规则表达式以及示例(选自项目readme):

JsonPath (点击测试) 结果
$.store.book[*].author The authors of all books
$..author All authors
$.store.* All things, both books and bicycles
$.store..price The price of everything
$..book[2] The third book
$..book[-2] The second to last book
$..book[0,1] The first two books
$..book[:2] All books from index 0 (inclusive) until index 2 (exclusive)
$..book[1:2] All books from index 1 (inclusive) until index 2 (exclusive)
$..book[-2:] Last two books
$..book[2:] Book number two from tail
$..book[?(@.isbn)] All books with an ISBN number
$.store.book[?(@.price < 10)] All books in store cheaper than 10
$..book[?(@.price <= $['expensive'])] All books in store that are not "expensive"
$..book[?(@.author =~ /.*REES/i)] All books matching regex (ignore case)
$..* Give me every thing
$..book.length() The number of books

实战测试

就用项目readme文档提供的例子进行测试:

json格式字符串,点击查看
{
"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
}

POM中引入依赖:

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

写一个单元测试类:

@SpringBootTest
@RunWith(MockitoJUnitRunner.class)
public class JsonPathTest {
private final String jsonStr = "...";
private final Object context = Configuration.defaultConfiguration().jsonProvider().parse(jsonStr); @Test
public void test(){}
}

其中:

  1. 第二个注解可以做到启动SpringBoot测试类而无需启动SpringApplication,并且也可以做到组件注入等功能。
  2. jsonStr用来指代之前所述的json字符串,这里简略写。
  3. context实际上是由json字符串通过默认配置生成的一个Map<String, Object>。

先看看context的类型:

System.out.println("context的类型:" + context.getClass().getSimpleName() + "\n");
------------------------------------------------------------------
output: context的类型:LinkedHashMap

用法1:通过key定位一个value:

jsonpath = "$.store.book[1].price";
result = JsonPath.read(testMap, jsonpath);
System.out.println("contents on " + jsonpath + ": " + result + "\n");
------------------------------------------------------------------
output: contents on $.store.book[1].price: 12.99

用法2:通过key定位一组value:

jsonpath = "$.store.book[*].price";
result = JsonPath.read(context, jsonpath);
System.out.println("contents on " + jsonpath + ": " + result + "\n");
------------------------------------------------------------------
output: contents on $.store.book[*].price: [8.95,12.99,8.99,22.99]

这里如果去查看result的type,会发现它是JSONArray类型,是List<Object>接口的实现,当List用就可以了。

用法3:向下递归查找:

jsonpath = "$..price";
result = JsonPath.read(context, jsonpath);
System.out.println("contents on " + jsonpath + ": " + result);
------------------------------------------------------------------
output: contents on $..price: [8.95,12.99,8.99,22.99,19.95]

在测试的时候想起一个问题,设想这样的场景,如果我们已经有了这样一个Map,只需要JsonPath提供的规则表达式解析与查找能力,那么想要使用JsonPath是不是需要Map->JsonString->context->规则解析->查找结果?答案是不需要的。

我们已经知道这个context本就是一个Map类型,那么是不是可以省去中间转为json string的步骤呢?我们测试看看:

//1. 先将json解析为一个Map
Map<String, Object> testMap = (Map) JSONObject.parseObject(JSON_STRING);
jsonpath = "$..price";
//2. 再将Map传入read方法,看是否能查找到结果:
result = JsonPath.read(testMap, jsonpath);
System.out.println("contents on " + jsonpath + ": " + result);
------------------------------------------------------------------
output: contents on $..price: [8.95,12.99,8.99,22.99,19.95]

可以看出,JsonPath的搜索规则解析能力是支持对普通Map进行查找的。

JsonPath:针对json的强大的规则解析与参数查找工具的更多相关文章

  1. 宜信开源|数据库审核软件Themis的规则解析与部署攻略

    一.介绍 Themis是宜信公司DBA团队开发的一款数据库审核产品,可帮助DBA.开发人员快速发现数据库质量问题,提升工作效率.其名称源自希腊神话中的正义与法律女神.项目取此名称,寓意此平台对数据库质 ...

  2. jsonpath对json数据进行分析校验做接口测试

    在做接口测试的时候, 我们需要对返回的数据进行分析校验, 一般返回的都是json格式的数据, 怎么来解析校验呢? 之前有看过使用递归遍历json数据的, 然后找到了jsonpath, 可以很方便的对j ...

  3. 爬虫Scrapy框架-Crawlspider链接提取器与规则解析器

    Crawlspider 一:Crawlspider简介 CrawlSpider其实是Spider的一个子类,除了继承到Spider的特性和功能外,还派生除了其自己独有的更加强大的特性和功能.其中最显著 ...

  4. Java开发笔记(一百零八)JSON串的定义和解析

    前面提到URL尾巴支持添加请求参数,具体格式形如“参数A名称=A参数值&参数B名称=B参数值”,可是这种格式只能传递简单的键值对信息,不能传递结构化数据,也无法传递数组形式的参数,因而它不适用 ...

  5. js中解析json对象:JSON.parse()用于从一个字符串中解析出json对象, JSON.stringify()用于从一个对象解析出字符串。

    JSON.parse()用于从一个字符串中解析出json对象. var str = '{"name":"huangxiaojian","age&quo ...

  6. jsonpath读取json数据格式公用方法!!!

    import java.util.LinkedHashMap; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.Pred ...

  7. 将一个JSON数组[{},{},{}]按一定规则合并到另一个JSON数组[{},{},{}]

    // 将一个JSON数组[{},{},{}]按一定规则合并到另一个JSON数组[{},{},{}] // Object.assign方法的第一个参数是目标对象,后面的参数都是源对象. var list ...

  8. JSON的简单使用_解析前台传来的JSON数据

    package cn.rocker.json; import org.junit.Test; import net.sf.json.JSONArray; import net.sf.json.JSON ...

  9. apache伪静态规则解析

    apache伪静态规则解析 最近有个客户有个要求,昨天折腾了一会,没解决,今天没啥就多学习学习 还是根据例子来学习比较快 1 简单的重定向规则 RewriteEngine On //启动规则 Rewr ...

随机推荐

  1. python学习-Day5

    目录 今日学习内容详解 流程控制理论 程序执行流程分类: 重点小知识* 分支结构(if 语法) 单 if 分支 if 与 else 分支 if 与 elif 与 else 分支 if嵌套 练习 循环结 ...

  2. ASP.NET Web 应用 Docker踩坑历程

    听说Docker这玩意挺长时间了,新建Web应用的时候,也注意到有个启用Docker的选项. 前两天扫了一眼<[大话云原生]煮饺子与docker.kubernetes之间的关系>,觉得有点 ...

  3. XCTF练习题---MISC---2017_Dating_in_Singapore

    XCTF练习题---MISC---2017_Dating_in_Singapore flag:HITB{CTFFUN} 解题步骤: 1.观察题目,下载附件 2.打开附件后发现是一张日历,还是新加坡的, ...

  4. Java多线程——实现

    "java多线程的实现--几乎都要和java.lang.Thread打交道" 方式一:继承于Thread类 1.创建一个继承于Thread类的子类 2.重写Thread类的run( ...

  5. OAuth 2.1 框架

    OAuth 2.1 Draft 当前版本:v2-1-05 失效时间:2022/09/08 本文对部分原文翻译,同时加了一些笔记,以便理解. 单词 译意 identifiler 识别码 Resource ...

  6. 探索 Python/Django 支持分布式多租户数据库,如 Postgres+Citus

    在 确定分布策略 中,我们讨论了在多租户用例中使用 Citus 所需的与框架无关的数据库更改. 在这里,我们专门研究如何借助 django-multitenant 库将多租户 Django 应 用程序 ...

  7. python入门基础知识二(字符串的常用操作方法)

    下标/索引: a = "I'm interested in Python." print(a[4]) i # 英文的字符串每一个下标/索引对应一个字母(含标点) a = '我喜欢p ...

  8. netty系列之:netty中常用的xml编码解码器

    目录 简介 XmlFrameDecoder XmlDecoder 总结 简介 在json之前,xml是最常用的数据传输格式,虽然xml的冗余数据有点多,但是xml的结构简单清晰,至今仍然运用在程序中的 ...

  9. 运维:ITSM

    IT服务管理(ITSM)是一套帮助企业对IT系统的规划.研发.实施和运营进行有效管理的方法,是一套方法论.ITSM起源于ITIL(IT Infrastructure Library,IT基础架构标准库 ...

  10. Spring Boot整合模板引擎jsp

    jsp也算是一种模板引擎吧.整合jsp前,先说一下运行SpringBoot项目的几种方式 1. 运行SpringBoot项目的几种方式 1.1 使用内嵌Tomcat运行项目 在IDE中右键运行启动类, ...