ParamUtils代码如下:
package com.ksource.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.ksource.modules.serve.entity.ServeInfo; import groovy.lang.Binding; import groovy.lang.GroovyShell; import groovy.lang.Script; import org.codehaus.groovy.control.CompilationFailedException; import java.util.*; /** * @Author dxy * @Date 2024/7/9 10:23 * @Description 参数处理工具类 */ public class ParamUtils { //工具类所在包1 public static final String packageName1 = "com.ksource.utils"; //工具类所在包2 public static final String packageName2 = "com.ksource.common.util"; /** * @description: 根据keyPath获取map中对应的value值 * @author: dxy * @date: 2024/7/17 17:02 * @param: keyPath * @param: mapData * @return: java.lang.Object */ public static Object getValueFromMapByKeyPath(String keyPath, Map<String,Object> mapData){ Object value = mapData.get(keyPath); if(keyPath.contains(".")) { String[] keys = keyPath.split("\\."); String mapStr = JSON.toJSONString(mapData); //处理map中的对象属性值 例:serveInfo.address;serveInfo.interfacingConfig.name等 if (keys.length > 1) { JSONObject jsonObj = JSONObject.parseObject(mapStr); JSONObject jsonObjX = jsonObj; for (int j = 0; j < keys.length; j++) { if (j < keys.length - 1) { jsonObjX = jsonObjX.getJSONObject(keys[j]); } else { value = jsonObjX.get(keys[keys.length - 1]); } } } } return value; } /** * @description: 根据keyPath获取jsonObject中对应的值 * @author: dxy * @date: 2024/7/17 17:02 * @param: keyPath * @param: jsonObject * @return: java.lang.Object */ public static Object getValueFromJsonByKeyPath(String keyPath, JSONObject jsonObject){ Object value = jsonObject.get(keyPath); if(keyPath.contains(".")) { String[] keys = keyPath.split("\\."); //处理jsonObject中的对象属性值 例:serveInfo.address;serveInfo.interfacingConfig.name等 if (keys.length > 1) { JSONObject jsonObjX = jsonObject; for (int j = 0; j < keys.length; j++) { if (j < keys.length - 1) { jsonObjX = jsonObjX.getJSONObject(keys[j]); } else { value = jsonObjX.get(keys[keys.length - 1]); } } } } return value; } /** * @description: 单个执行方法 * @author: dxy * @date: 2024/7/17 17:03 * @param: javaStrCode * @param: mapData * @return: java.lang.Object */ public static Object execJavaStrCodeByGroovy(String javaStrCode, Map<String,Object> mapData){ String clazzName = javaStrCode.substring(0,javaStrCode.indexOf(".")); String clazzPath = getPathByClassName(clazzName); String methodParam = javaStrCode.substring(javaStrCode.indexOf(".")); String newCodeStr = clazzPath + methodParam; Binding binding = new Binding(mapData); GroovyShell shell = new GroovyShell(binding); try { Script parse = shell.parse(newCodeStr); Object run = parse.run(); return run; } catch (CompilationFailedException e) { throw new RuntimeException("执行动态脚本【"+newCodeStr+"】失败!",e); } } /** * @description: 根据包名+类名获取对应的clazz * @author: dxy * @date: 2024/7/9 13:50 * @param: className * @return: java.lang.Class<?> */ public static String getPathByClassName(String clazzName){ String flag = null; String clazzPath = null; try { Class.forName(packageName1+"."+clazzName); clazzPath = packageName1+"."+clazzName; flag = "exist"; }catch (Exception e){ flag = "noExist"; } if("noExist".equals(flag)){ try { Class.forName(packageName2+"."+clazzName); clazzPath = packageName2+"."+clazzName; flag = "exist"; }catch (Exception e){ flag = "noExist"; } } return clazzPath; } /** * @description: 执行多个方法 * @author: dxy * @date: 2024/7/17 17:03 * @param: transformConfig * @param: sourceMap * @return: java.util.Map<java.lang.String,java.lang.Object> */ public static Map<String,Object> buildTargetMapFromSourceMap(JSONArray transformConfig, Map<String,Object> sourceMap){ Map<String,Object> newMap = new HashMap<String,Object>(); for(int i = 0; i < transformConfig.size(); i++){ JSONObject jsonObject = transformConfig.getJSONObject(i); boolean isOut = jsonObject.getBoolean("isOut"); String key = jsonObject.getString("name"); String methodAttr = jsonObject.getString("value"); Object object = execJavaStrCodeByGroovy(methodAttr, sourceMap); sourceMap.put(key,object); if (isOut){ newMap.put(key,object); } } return newMap; } /** * @description: 对指定属性赋值 * @author: jyf * @date: 2024/7/17 上午10:17 * @param: jsonObject * @param: key * @param: value */ public static void setJsonObjectValue(JSONObject jsonObject, String key, Object value) { String[] keys = key.split("\\."); JSONObject currentObject = jsonObject; for (int i = 0; i < keys.length - 1; i++) { String currentKey = keys[i]; if (!currentObject.containsKey(currentKey) || currentObject.get(currentKey) == null) { currentObject.put(currentKey, new JSONObject()); } currentObject = currentObject.getJSONObject(currentKey); } currentObject.put(keys[keys.length - 1], value); } public static void main(String[] args) { JSONObject jsonObject = new JSONObject(); ServeInfo serveInfo1 = new ServeInfo(); serveInfo1.setAddress("河南省郑州市中原区"); jsonObject.put("name","张华"); jsonObject.put("serveInfo1",serveInfo1); //根据key路径获取jsonObject的值 Object obj1 = getValueFromJsonByKeyPath("serveInfo1.address",jsonObject); System.out.println(obj1); Map testMap = new HashMap(); testMap.put("name","李可"); testMap.put("age",15); //根据key路径获取map的值 Object obj2 = getValueFromMapByKeyPath("name",testMap); System.out.println(obj2); //单方法执行 String groovyScript = "TestJava.a2(name,age)"; Object obj3 = execJavaStrCodeByGroovy(groovyScript, testMap); System.out.println(obj3); Map map = new HashMap(); map.put("name","陈华"); map.put("age",25); map.put("address","河南省"); map.put("unit","上海通方系统有限公司"); List<ServeInfo> serveInfoList = new ArrayList(); ServeInfo serveInfo = new ServeInfo(); serveInfo.setAddress("中原区"); map.put("serveInfoList",serveInfoList); map.put("serveInfo",serveInfo); JSONArray jsonArray = new JSONArray(); JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("name","test1"); jsonObject1.put("value","TestJava.a14(name)"); jsonObject1.put("isOut",true); JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("name","test2"); jsonObject2.put("value","TestJava.a15(test1)"); jsonObject2.put("isOut",true); JSONObject jsonObject3 = new JSONObject(); jsonObject3.put("name","test3"); jsonObject3.put("value","TestJava.a15(test2)"); jsonObject3.put("isOut",false); JSONObject jsonObject4 = new JSONObject(); jsonObject4.put("name","test4"); jsonObject4.put("value","TestJava.a16(test2,'杨金路')"); jsonObject4.put("isOut",true); jsonArray.add(jsonObject1); jsonArray.add(jsonObject2); jsonArray.add(jsonObject3); jsonArray.add(jsonObject4); //多方法执行 Map newMap= ParamUtils.buildTargetMapFromSourceMap(jsonArray, map); System.out.println(newMap); } }标签:Groovy,String,处理,JSONObject,keys,参数,jsonObject,put,new From: https://www.cnblogs.com/henuyuxiang/p/18307977