/** * 方法描述 把實體對象中的value值進行加密後轉化成JSON * * author xingMeiLing * param object待解析對象 publicKey公鑰 * return json(key是屬性,value是加密後的值) */ public String getEntityValue(Object object, PublicKey publicKey){ Class clazz = object.getClass(); // 獲取類中聲明的字段 Field[] fields = clazz.getDeclaredFields(); // 拼接String須要 StringBuilder sb = new StringBuilder(); sb.append("{"); for (Field field : fields) { // 避免 can not access a member of class com.java.test.Person with modifiers "private" field.setAccessible(true); try { // 獲取實體屬性的值,經過公鑰把返回的值進行加密 // publicKeyBase64()這個加密方法略 String base64Value = publicKeyBase64(field.get(object),publicKey); // 進行拼接 sb.append("\""); // 獲取實體屬性的Name sb.append(field.getName()); sb.append("\""); sb.append(":"); sb.append("\""); sb.append(base64Value); sb.append("\""); sb.append(","); System.out.println(field.getName() + ":"+ field.get(object)); } catch (IllegalAccessException e) { log.error("加密數據異常"); e.printStackTrace(); } catch (Exception e) { log.error("公鑰加密異常",e.getMessage()); e.printStackTrace(); } } sb.replace(sb.length() - 1, sb.length(), ""); sb.append("}"); return String.valueOf(sb); }