You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
2.1 KiB
63 lines
2.1 KiB
package com.bfd.utils;
|
|
|
|
import java.util.Map;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONPath;
|
|
import com.bfd.entity.Constants;
|
|
|
|
/**
|
|
* @author:jinming
|
|
* @className:DataUtil
|
|
* @version:1.0
|
|
* @description: 获取dataValue的值
|
|
* @Date:2023/11/1 9:54
|
|
*/
|
|
@Slf4j
|
|
public class DataUtil {
|
|
/**
|
|
*
|
|
* @param key 传入的key
|
|
* @param dataMap 数据map
|
|
* @return 根据传入的参数进行判断解析,返回正确的dataValue
|
|
*/
|
|
public static Object getValue(String key, Map dataMap) {
|
|
try {
|
|
//公式为空直接就返回
|
|
if(key.equals(Constants.EMPTY)){
|
|
return Constants.EMPTY;
|
|
}
|
|
Object dataValue;
|
|
String isJson = "#json#";
|
|
if (key.contains(isJson)) {
|
|
//进行第一次拆分,获取#json#前面的部分
|
|
String[] keySplit = key.split(isJson);
|
|
String firstDataKey = keySplit[0];
|
|
String[] firstDataKeySplit = firstDataKey.split(":");
|
|
//取出前半部分对应的JSON数据并转换为JSONObject
|
|
String dataJson = (String) dataMap.get(firstDataKeySplit[0]);
|
|
JSONObject dataJsonObject = JSON.parseObject(dataJson);
|
|
//根据key的后半部分取出对应JSONObject中的值
|
|
String firstDataKeyJson = (String) JSONPath.eval(dataJsonObject, firstDataKeySplit[1]);
|
|
String secDataKey = keySplit[1];
|
|
JSONObject firstDataJsonObject = JSON.parseObject(firstDataKeyJson);
|
|
dataValue = JSONPath.eval(firstDataJsonObject, secDataKey);
|
|
return dataValue;
|
|
}
|
|
String[] keySplit = key.split(":");
|
|
String jsonPath = keySplit[1];
|
|
String dataJson = (String) dataMap.get(keySplit[0]);
|
|
JSONObject dataJsonObject = JSON.parseObject(dataJson);
|
|
dataValue = JSONPath.eval(dataJsonObject, jsonPath);
|
|
return dataValue;
|
|
} catch (Exception e) {
|
|
// TODO: handle exception
|
|
log.error("jsonpath公式取值异常,",e);
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|