2022-08-04 10:16:08 +08:00
|
|
|
|
package com.zdjizhi.utils.json;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.log.Log;
|
|
|
|
|
|
import cn.hutool.log.LogFactory;
|
|
|
|
|
|
import com.jayway.jsonpath.InvalidPathException;
|
|
|
|
|
|
import com.jayway.jsonpath.JsonPath;
|
|
|
|
|
|
import com.zdjizhi.utils.StringUtil;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @author qidaijie
|
|
|
|
|
|
* @Package com.zdjizhi.utils.json
|
|
|
|
|
|
* @Description:
|
|
|
|
|
|
* @date 2022/7/1817:19
|
|
|
|
|
|
*/
|
|
|
|
|
|
public class JsonPathUtil {
|
|
|
|
|
|
private static final Log logger = LogFactory.get();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 通过 josnPath 解析,返回String类型数据
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param message json数据
|
|
|
|
|
|
* @param expr 解析表达式
|
|
|
|
|
|
* @return 返回值
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String getStringValue(String message, String expr) {
|
|
|
|
|
|
String result = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (StringUtil.isNotBlank(message) && StringUtil.isNotBlank(expr)) {
|
2022-08-26 11:46:10 +08:00
|
|
|
|
ArrayList<Object> read = JsonPath.parse(message).read(expr);
|
2022-08-04 10:16:08 +08:00
|
|
|
|
if (read.size() >= 1) {
|
2022-08-26 11:46:10 +08:00
|
|
|
|
result = read.get(0).toString();
|
2022-08-04 10:16:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
|
logger.error("JSONPath parsing json returns String data exception" + e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 通过 josnPath 解析,返回Long类型数据
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param message json数据
|
|
|
|
|
|
* @param expr 解析表达式
|
|
|
|
|
|
* @return 返回值
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static Integer getIntegerValue(String message, String expr) {
|
|
|
|
|
|
Integer result = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (StringUtil.isNotBlank(message) && StringUtil.isNotBlank(expr)) {
|
2022-08-26 11:46:10 +08:00
|
|
|
|
ArrayList<Object> read = JsonPath.parse(message).read(expr);
|
2022-08-04 10:16:08 +08:00
|
|
|
|
if (read.size() >= 1) {
|
2022-08-26 11:46:10 +08:00
|
|
|
|
result = Integer.parseInt(read.get(0).toString());
|
2022-08-04 10:16:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
|
logger.error("JSONPath parsing json returns Long data exception" + e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 通过 josnPath 解析,返回Long类型数据
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param message json数据
|
|
|
|
|
|
* @param expr 解析表达式
|
|
|
|
|
|
* @return 返回值
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static Long getLongValue(String message, String expr) {
|
|
|
|
|
|
Long result = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (StringUtil.isNotBlank(message) && StringUtil.isNotBlank(expr)) {
|
2022-08-26 11:46:10 +08:00
|
|
|
|
ArrayList<Object> read = JsonPath.parse(message).read(expr);
|
2022-08-04 10:16:08 +08:00
|
|
|
|
if (read.size() >= 1) {
|
2022-08-26 11:46:10 +08:00
|
|
|
|
result = Long.parseLong(read.get(0).toString());
|
2022-08-04 10:16:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (RuntimeException e) {
|
2022-08-26 11:46:10 +08:00
|
|
|
|
logger.error("JSONPath parsing json returns Long data exception: " + e);
|
2022-08-04 10:16:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|