这篇文章主要介绍“JSONPATH json解析工具的使用”,在日常操作中,相信很多人在JSONPATH json解析工具的使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”JSONPATH json解析工具的使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
1、jsonPath的在github上的网址如下:https://github.com/json-path/JsonPath
2、json-path 快速入门
一、json-path中的操作符
二、json-path中可以使用的函数
三、过滤操作符
3、maven依赖
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
4、util 代码
package com.ysma.ppt.util.resource;
import com.jayway.jsonpath.*;
import com.jayway.jsonpath.spi.json.JsonSmartJsonProvider;
import com.ysma.ppt.intf.pojo.TemplateDO;
import org.springframework.cglib.beans.BeanMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author ysma 2019-09-25
* JsonPath工具类
* JsonPath表达式可以使用点表示法:$.store.book[0].title
* 或括号表示法:$['store']['book'][0]['title']
*
* real_param_response表path字段存储格式仿点表示法,如store.book[1].isbn
*/
public class JsonPathUtil {
//JsonPath中的“根成员对象”始终称为$,无论是对象还是数组
private static final String ROOT_PREFIX = "$";
private static Configuration configuration;
static {
configuration = Configuration.builder().options(
Option.DEFAULT_PATH_LEAF_TO_NULL, // 如果路径不存在则返回null,而不要抛出PathNotFoundException
Option.SUPPRESS_EXCEPTIONS // 抑制异常的抛出,当设置了Option.ALWAYS_RETURN_LIST时返回[],否则返回null
).jsonProvider(new JsonSmartJsonProvider()).build();
}
/**
* 解析类
* @param resJsonStr 待解析的返参对象
* @param expectList 定义的预期结果集合
* @return 结果集
*/
public static Map<String, Object> parseJson(String resJsonStr, List<BeanMap> expectList){
/*1.此处预先解析json,默认请情下JsonPath.read方法每掉一次都会重新解析json,此处预先解析好就不用每次都进行解析*/
DocumentContext context = JsonPath.parse(resJsonStr, configuration);
//2.构造返回结果
Map<String, Object> resultMap = new HashMap<>();
expectList.forEach(beanMap -> {
String path = String.join(".", ROOT_PREFIX, (String)beanMap.get("path"));
//beanMap.get("dataType") 数据类型的作用弱化了
Object val = context.read(path);
resultMap.put((String)beanMap.get("code"), val);
});
return resultMap;
}
/**groovy脚本中可使用此定制开发*/
public static Map<String, Object> parsePathJson(String resJsonStr, List<Map<String, String>> pathList){
/*1.此处预先解析json,默认请情下JsonPath.read方法每掉一次都会重新解析json,此处预先解析好就不用每次都进行解析*/
DocumentContext context = JsonPath.parse(resJsonStr, configuration);
//2.构造返回结果
Map<String, Object> resultMap = new HashMap<>();
pathList.forEach(pathMap -> {
String path = String.join(".", ROOT_PREFIX, pathMap.get("path"));
//beanMap.get("dataType") 数据类型的作用弱化了
Object val = context.read(path);
resultMap.put(pathMap.get("code"), val);
});
return resultMap;
}
/**
* https://www.baeldung.com/guide-to-jayway-jsonpath
* 官网地址,可查询过滤器定义功能等
*/
private static void testParse(String resJsonStr, List<BeanMap> expectList){
Object obj = configuration.jsonProvider().parse(resJsonStr);
expectList.forEach(beanMap -> {
String path = String.join(".", ROOT_PREFIX, (String)beanMap.get("path"));
Object read = JsonPath.read(obj, path, Filter.filter(Criteria.where("price").lt(5.5)));
System.out.println("read:"+read);
});
}
public static void main(String[] args) {
List<TemplateDO> responseDOS = new ArrayList<>();
TemplateDO rd = new TemplateDO();
rd.setCode("color");
rd.setPath("store.bicycle[?]");
rd.setDataType("double");
responseDOS.add(rd);
/*ParamResponseRealDO rd2 = new ParamResponseRealDO();
rd2.setCode("category");
rd2.setPath("hehe.store.book[*].category");
rd2.setDataType("array");
responseDOS.add(rd2);*/
List<BeanMap> expectList = responseDOS.stream().map(BeanMap::create).collect(Collectors.toList());
String respJson = getRespJson();
/*Map<String, Object> resultMap = parseJson(respJson, expectList);
System.out.println(JSON.toJSONString(resultMap));*/
testParse(respJson, expectList);
}
private static String getRespJson(){
return "{ \"store\": {\n">
5、官网中说明了 过滤器的具体使用规则,为具体研发提供了很大的自由度和帮助
如testParse方法中Criteria的使用就是基于store.bicycle[?] 语义才可以继续的。多一步少一步都不行
参考:
https://blog.csdn.net/fu_huo_1993/article/details/88350147 给出了jsonpath的地址和api简图,非常好
https://www.baeldung.com/guide-to-jayway-jsonpath 给出了官网中对应的定义 非常好
到此,关于“JSONPATH json解析工具的使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/ysma1987/blog/3116269