JSON 、复杂对象转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | public static Object copyProperties(Object jsonObject, Object desObj) { //1. 首先要拿到oriObj对象的所有存在值不为空的键-值,放入到集合当中 if (jsonObject instanceof JSONObject) { JSONObject oriObj = (JSONObject) jsonObject; Class desC = desObj.getClass(); Field[] oField = desC.getDeclaredFields(); Class pclazz = desC.getSuperclass(); Field[] pFields = pclazz.getDeclaredFields(); int osize = oField.length; int psize = pFields.length; Field[] desFields = new Field[osize+ psize]; for ( int i= 0 ; i <osize+ psize ; i ++ ){ if (i < osize ){ desFields[i] = oField[i]; } else { desFields[i] = pFields[i -osize ]; } } for (Field desField : desFields) { String fieldName = desField.getName(); String getMethodName = "get" + fieldName.substring( 0 , 1 ).toUpperCase() + fieldName.substring( 1 , fieldName.length()); String setMethodName = "set" + fieldName.substring( 0 , 1 ).toUpperCase() + fieldName.substring( 1 , fieldName.length()); if (oriObj.get(fieldName) != null ) { Object or = oriObj.get(fieldName); if (or instanceof JSONObject) { try { Class type = desField.getType(); if (type.getName().indexOf( "[" ) == 0 ) { continue ; } Method getMethod = desC.getMethod(getMethodName, null ); Object ch = getMethod.invoke(desObj); if (or instanceof JSONObject || or instanceof JSONArray) { copyProperties(or, ch); } Method setMethod = desObj.getClass().getMethod(setMethodName, type); setMethod.invoke(desObj, ch); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } else if (or instanceof JSONArray) { try { Class type = desField.getType(); if (type.getName().indexOf( "[" ) != 0 ) { continue ; } Method getMethod = desC.getMethod(getMethodName, null ); JSONArray jsonArray = (JSONArray) or; Object[] o = (Object[]) getMethod.invoke(desObj); if (o == null ){ Class ctype = Class.forName(type.getName().replace( "[L" , "" ).replace( ";" , "" )); o = (Object[]) Array.newInstance(ctype,jsonArray.size()); for ( int i = 0 ; i < o.length; i ++){ o[i] = ctype.newInstance(); } } Object o1 = copyProperties(jsonArray, o); Method setMethod = desObj.getClass().getMethod(setMethodName, type); setMethod.invoke(desObj, o1); } catch (Exception e) { e.printStackTrace(); } } else { try { Class type = desField.getType(); Object pa = oriObj.getObject(fieldName,type); Method setMethod = desObj.getClass().getMethod(setMethodName, type); setMethod.invoke(desObj, pa ); } catch (Exception e) { e.printStackTrace(); } } } } } else if (jsonObject instanceof JSONArray){ JSONArray jsonArray = (JSONArray) jsonObject; try { Class type = desObj.getClass(); if (type.getName().indexOf( "[" ) != 0 ) { return null ; } Object[] ob = (Object[]) desObj; Integer size = jsonArray.size(); if (ob.length != size){ ob = (Object[]) Array.newInstance(type,size); } for ( int i = 0 ; i< size ; i ++ ){ Object o = jsonArray.get(i); if (o instanceof JSONObject) { // Object co = Class.forName(type.getCanonicalName().replace("[","").replace("]","")).newInstance(); ob[i] = copyProperties(o, ob[i] );; } } return ob; } catch (Exception e) { e.printStackTrace(); } } return desObj; } public static void wirteJson (Object desObj, JSONObject retJson) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { //1. 首先要拿到oriObj对象的所有存在值不为空的键-值,放入到集合当中 String a = "" ; Class desC = desObj.getClass(); Field[] desFields = desC.getDeclaredFields(); Class pclazz = desC.getSuperclass(); Field[] pFields = pclazz.getDeclaredFields(); for (Field desField : desFields) { Class c = desField.getType(); String fieldName = desField.getName(); String getMethodName = "get" + fieldName.substring( 0 , 1 ).toUpperCase() + fieldName.substring( 1 , fieldName.length()); try { Method getMethod = desC.getMethod(getMethodName, null ); if (c.getName().indexOf( "[" ) == 0 ) { /** * t */ Object[] obs = (Object[]) getMethod.invoke(desObj); //获取得到该属性的取值 if (obs == null ) { continue ; } JSONArray cjson = new JSONArray(); for (Object ob : obs) { JSONObject cn = new JSONObject(); wirteJson(ob, cn); cjson.add(cn); } retJson.put(fieldName, cjson); } else { Object ob = getMethod.invoke(desObj); //获取得到该属性的取值 if (ob == null ) { continue ; } JSONObject cjson = new JSONObject(); Field[] cfs = ob.getClass().getDeclaredFields(); boolean b = false ; for (Field cf : cfs) { String cfieldName = cf.getName(); String getCMethodName = "get" + cfieldName.substring( 0 , 1 ).toUpperCase() + cfieldName.substring( 1 , cfieldName.length()); try { Method getCMethod = ob.getClass().getMethod(getCMethodName); b = true ; break ; } catch (Exception e) { } } if (b) { wirteJson(ob, cjson); retJson.put(fieldName, cjson); } retJson.put(fieldName, ob); } } catch (Exception e) { logger.error(fieldName + "get方法获取失败" ); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」