這篇文章主要介紹“JSONPATH json解析工具的使用”,在日常操作中,相信很多人在JSONPATH json解析工具的使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”JSONPATH json解析工具的使用”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、信豐ssl等。為1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學管理、有技術(shù)的信豐網(wǎng)站制作公司
1、jsonPath的在github上的網(wǎng)址如下:https://github.com/json-path/JsonPath
2、json-path 快速入門
一、json-path中的操作符

二、json-path中可以使用的函數(shù)

三、過濾操作符

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中的“根成員對象”始終稱為$,無論是對象還是數(shù)組
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 // 抑制異常的拋出,當設(shè)置了Option.ALWAYS_RETURN_LIST時返回[],否則返回null
).jsonProvider(new JsonSmartJsonProvider()).build();
}
/**
* 解析類
* @param resJsonStr 待解析的返參對象
* @param expectList 定義的預期結(jié)果集合
* @return 結(jié)果集
*/
public static Map<String, Object> parseJson(String resJsonStr, List<BeanMap> expectList){
/*1.此處預先解析json,默認請情下JsonPath.read方法每掉一次都會重新解析json,此處預先解析好就不用每次都進行解析*/
DocumentContext context = JsonPath.parse(resJsonStr, configuration);
//2.構(gòu)造返回結(jié)果
Map<String, Object> resultMap = new HashMap<>();
expectList.forEach(beanMap -> {
String path = String.join(".", ROOT_PREFIX, (String)beanMap.get("path"));
//beanMap.get("dataType") 數(shù)據(jù)類型的作用弱化了
Object val = context.read(path);
resultMap.put((String)beanMap.get("code"), val);
});
return resultMap;
}
/**groovy腳本中可使用此定制開發(fā)*/
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.構(gòu)造返回結(jié)果
Map<String, Object> resultMap = new HashMap<>();
pathList.forEach(pathMap -> {
String path = String.join(".", ROOT_PREFIX, pathMap.get("path"));
//beanMap.get("dataType") 數(shù)據(jù)類型的作用弱化了
Object val = context.read(path);
resultMap.put(pathMap.get("code"), val);
});
return resultMap;
}
/**
* https://www.baeldung.com/guide-to-jayway-jsonpath
* 官網(wǎng)地址,可查詢過濾器定義功能等
*/
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、官網(wǎng)中說明了 過濾器的具體使用規(guī)則,為具體研發(fā)提供了很大的自由度和幫助
如testParse方法中Criteria的使用就是基于store.bicycle[?] 語義才可以繼續(xù)的。多一步少一步都不行
參考:
https://blog.csdn.net/fu_huo_1993/article/details/88350147 給出了jsonpath的地址和api簡圖,非常好
https://www.baeldung.com/guide-to-jayway-jsonpath 給出了官網(wǎng)中對應(yīng)的定義 非常好
到此,關(guān)于“JSONPATH json解析工具的使用”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
網(wǎng)站欄目:JSONPATHjson解析工具的使用
標題鏈接:http://chinadenli.net/article10/ggphgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、營銷型網(wǎng)站建設(shè)、用戶體驗、網(wǎng)站內(nèi)鏈、服務(wù)器托管、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)