https://blog.csdn.net/qq_39750658/article/details/83411897
我也是参考了这个作者。顶他
public static JSONObject changeJsonObj(JSONObject jsonObj, ListkeyMap ,String FatherName) { JSONObject resJson = new JSONObject(); Set keySet = jsonObj.keySet(); for (String key : keySet) { // String resKey = keyMap.get(key) == null ? key : keyMap.get(key); String resKey =resultKey(keyMap,key,FatherName) == null ? key : resultKey(keyMap,key,FatherName); try { JSONObject jsonobj1 = jsonObj.getJSONObject(key); resJson.put(resKey, changeJsonObj(jsonobj1, keyMap,key)); } catch (Exception e) { try { JSONArray jsonArr = jsonObj.getJSONArray(key); resJson.put(resKey, changeJsonArr(jsonArr, keyMap,key)); } catch (Exception x) { resJson.put(resKey, jsonObj.get(key)); } } } return resJson; } public static JSONArray changeJsonArr(JSONArray jsonArr, List keyMap,String clienteleFatherNode) { JSONArray resJson = new JSONArray(); for (int i = 0; i < jsonArr.size(); i++) { JSONObject jsonObj = jsonArr.getJSONObject(i); resJson.add(changeJsonObj(jsonObj, keyMap,clienteleFatherNode)); } return resJson; }
private static Listresult( List keyMap){ TestVO test1=new TestVO(); test1.setConversionName("XingMing"); test1.setOriginalFatherName("user"); test1.setOriginalName("name"); TestVO test2=new TestVO(); test2.setConversionName("YongHu"); test2.setOriginalFatherName(""); test2.setOriginalName("user"); TestVO test3=new TestVO(); test3.setConversionName("MiaoShu"); test3.setOriginalFatherName("hobby"); test3.setOriginalName("desc"); TestVO test4=new TestVO(); test4.setConversionName("dengyan"); test4.setOriginalFatherName("user"); test4.setOriginalName("desc"); TestVO test5=new TestVO(); test5.setConversionName("DY"); test5.setOriginalFatherName(""); test5.setOriginalName("desc"); keyMap.add(test3); keyMap.add(test2); keyMap.add(test1); keyMap.add(test4); keyMap.add(test5); return keyMap; } public static void main(String[] args) { String jsonStr = "{\"user\":{\"name\":\"张三\",\"desc\":\"就是这么任性\",\"sex\":\"男\",\"hobby\":[{\"motion\":\"足球\",\"desc\":\"任性\"},{\"game\":\"英雄联盟\",\"desc\":\"就是这么任性\"}]},\"desc\":\"aaaaaaa\"}"; List keyMap=new ArrayList<>(); keyMap =result(keyMap); JSONObject jsonObj = JSONObjectConversionUtil.changeJsonObj(JSONObject.parseObject(jsonStr),keyMap,""); System.out.println("换值结果 》》 " + jsonObj.toString()); }
public class TestVO { //原来的父类名 private String originalFatherName; //需要转化的名称 private String ConversionName; //原来的名称 private String originalName;
}