JsonPath:针对json的强大的规则解析与参数查找工具
项目特点
GitHub项目地址:https://github.com/json-path/JsonPath
主要功能:
- 将Json字符串转为Java Map对象(这个不算什么,FastJson之类的工具都可以)
- 通过强大的规则表达式定位字段,返回字段值或值集合(很厉害)
支持的规则表达式以及示例(选自项目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(){}
}
其中:
- 第二个注解可以做到启动SpringBoot测试类而无需启动SpringApplication,并且也可以做到组件注入等功能。
- jsonStr用来指代之前所述的json字符串,这里简略写。
- 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的强大的规则解析与参数查找工具的更多相关文章
- 宜信开源|数据库审核软件Themis的规则解析与部署攻略
一.介绍 Themis是宜信公司DBA团队开发的一款数据库审核产品,可帮助DBA.开发人员快速发现数据库质量问题,提升工作效率.其名称源自希腊神话中的正义与法律女神.项目取此名称,寓意此平台对数据库质 ...
- jsonpath对json数据进行分析校验做接口测试
在做接口测试的时候, 我们需要对返回的数据进行分析校验, 一般返回的都是json格式的数据, 怎么来解析校验呢? 之前有看过使用递归遍历json数据的, 然后找到了jsonpath, 可以很方便的对j ...
- 爬虫Scrapy框架-Crawlspider链接提取器与规则解析器
Crawlspider 一:Crawlspider简介 CrawlSpider其实是Spider的一个子类,除了继承到Spider的特性和功能外,还派生除了其自己独有的更加强大的特性和功能.其中最显著 ...
- Java开发笔记(一百零八)JSON串的定义和解析
前面提到URL尾巴支持添加请求参数,具体格式形如“参数A名称=A参数值&参数B名称=B参数值”,可是这种格式只能传递简单的键值对信息,不能传递结构化数据,也无法传递数组形式的参数,因而它不适用 ...
- js中解析json对象:JSON.parse()用于从一个字符串中解析出json对象, JSON.stringify()用于从一个对象解析出字符串。
JSON.parse()用于从一个字符串中解析出json对象. var str = '{"name":"huangxiaojian","age&quo ...
- jsonpath读取json数据格式公用方法!!!
import java.util.LinkedHashMap; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.Pred ...
- 将一个JSON数组[{},{},{}]按一定规则合并到另一个JSON数组[{},{},{}]
// 将一个JSON数组[{},{},{}]按一定规则合并到另一个JSON数组[{},{},{}] // Object.assign方法的第一个参数是目标对象,后面的参数都是源对象. var list ...
- JSON的简单使用_解析前台传来的JSON数据
package cn.rocker.json; import org.junit.Test; import net.sf.json.JSONArray; import net.sf.json.JSON ...
- apache伪静态规则解析
apache伪静态规则解析 最近有个客户有个要求,昨天折腾了一会,没解决,今天没啥就多学习学习 还是根据例子来学习比较快 1 简单的重定向规则 RewriteEngine On //启动规则 Rewr ...
随机推荐
- 使用fastai训练的一个性别识别模型
在学习了python中的一些机器学习的相关模块后,再一次开始了深度学习之旅.不过与上次的TensorFlow框架不同,这一次接触的是fast.ai这样一个东西.这个框架还不稳定,网上也没有相关的中文文 ...
- 附011.常见Linux镜像站点大全
开源系统镜像站点 国内Mirrors站点 企业类站点 阿里巴巴开源Mirrors站点:https://developer.aliyun.com/mirror/ 腾讯开源Mirrors站点:https: ...
- 重新审视C# Span<T>数据结构
先谈一下我对Span的看法, span是指向任意连续内存空间的类型安全.内存安全的视图. Span和Memory都是包装了可以在pipeline上使用的结构化数据的内存缓冲器,他们被设计用于在pipe ...
- 实验:Python图形图像处理
1. 准备一张照片,编写Python程序将该照片进行图像处理,分别输出以下效果的图片:(a)灰度图:(b)轮廓图: (c)变换RGB通道图:(d)旋转45度图. 2. 假设当前文件夹中data.csv ...
- python操作MySQL,SQL注入的问题,SQL语句补充,视图触发器存储过程,事务,流程控制,函数
python操作MySQL 使用过程: 引用API模块 获取与数据库的连接 执行sql语句与存储过程 关闭数据库连接 由于能操作MySQL的模块是第三方模块,我们需要pip安装. pip3 insta ...
- k8s中应用GlusterFS类型StorageClass
GlusterFS在Kubernetes中的应用 GlusterFS服务简介 GlusterFS是一个可扩展,分布式文件系统,集成来自多台服务器上的磁盘存储资源到单一全局命名空间,以提供共享文件存储. ...
- c# DirectoryEntry LDAPS
参考地址:https://stackoverflow.com/questions/54987776/ldap-connection-error-the-server-is-not-operationa ...
- 个人冲刺(三)——体温上报app(二阶段)
冲刺任务:完成用户类.温度数据和第二页面类的编写 User.java package com.example.helloworld; class User { private String usern ...
- 高危!Fastjson反序列化远程代码执行漏洞风险通告,请尽快升级
据国家网络与信息安全信息通报中心监测发现,开源Java开发组件Fastjson存在反序列化远程代码执行漏洞.攻击者可利用上述漏洞实施任意文件写入.服务端请求伪造等攻击行为,造成服务器权限被窃取.敏感信 ...
- mybatis if判断等于某个字符串
这种写法是错误的:在OGNL的表达式中,'true'会被解析成字符,因为java是强类型的 <if test="flag=='true' "> AND ho.id = ...