项目特点

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. Maven基础学习笔记

    Maven基础学习笔记 下载链接 官网:https://maven.apache.org/ 所有版本:https://archive.apache.org/dist/maven/maven-3/ 阿里 ...

  2. 「BUAA OO Unit 2 HW8」第二单元总结

    「BUAA OO Unit 2 HW8」第二单元总结 目录 「BUAA OO Unit 2 HW8」第二单元总结 Part 0 前言 Part 1 第五次作业 1.1 作业要求 1.2 架构设计 1. ...

  3. 1.4 类UNIX系统是什么鬼?

    上节<UNIX和Linux的区别>中讲到了 UNIX 系统的历史,UNIX 是操作系统的开山鼻祖,是操作系统的发源地,后来的 Windows 和 Linux 都参考了 UNIX. 有人说, ...

  4. .NET混合开发解决方案12 网页JS调用C#方法访问WinForm或WPF窗体

    系列目录     [已更新最新开发文章,点击查看详细] WebView2控件应用详解系列博客 .NET桌面程序集成Web网页开发的十种解决方案 .NET混合开发解决方案1 WebView2简介 .NE ...

  5. 846. Hand of Straights - LeetCode

    Question 846. Hand of Straights Solution 题目大意:打牌,判断牌是否能全部按顺子出 思路:构造一个list,存储1,2,3,4,5,6,7,8并排序,构造一个m ...

  6. Property or method "xxx" is not defined on the instance but referenced during render

    是xxx中的data写成date了,因此报错. 这个错误属于粗心

  7. salesforce零基础学习(一百一十五)记一个有趣的bug

    本篇参考:https://help.salesforce.com/s/articleView?language=en_US&type=1&id=000319486 page layou ...

  8. 意味着JNPF迈入新时代的3.4版本,与3.3.3版本有着哪些功能区别呢?

    在线开发‍ 3.3.3版本 同一个功能分功能设计和移动设计 功能设计没有更换模式 功能设计没有同步菜单 功能设计和移动设计无表模式 3.4.1版本 同一个功能可以在功能设计里面设计,根据客户需求自己选 ...

  9. django框架6

    内容概要 神奇的双下划线查询 外键字段的创建 外键字段操作 多表查询 基于对象的跨表查询 基于双下划线的跨表查询 双下线查询扩展 如何查看SQL语句 内容详情 神奇的双下划线查询 1.查询年龄大于20 ...

  10. Linux系统下运行.sh文件

    在Linux系统下运行.sh文件有两种方法,比如我在root目录下有个vip666.sh文件 #chmod +x *.sh的文件名 #./*.sh的文件名 第一种(这种办法需要用chmod使得文件具备 ...