import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.List; import java.util.Set; /** * @description: JSONObject參數遞歸轉換 * @author: huang wei * @create: 2020-12-02 11:50 */ public class JsonTrans { /** * 日志對象 */ private static final Logger LOG = LoggerFactory.getLogger(JsonTrans.class); public static void main(String[] args) { // 要轉換的參數對象 String responseStr = "{\"ret\":\"0\",\"count\":1,\"retMsg\":null,\"processCost\":null,\"data\":{\"moniid\":\"1000003\",\"monitype\":\"2\",\"moniname\":\"離線視屏-1\",\"location\":\"3\",\"locationName\":\"徐州市轄區公安局\",\"rtspurl\":\"/files/videoFile/1000003/1540863941796.mp4\",\"monistatus\":\"1\",\"createTime\":1540863941000,\"updateTime\":1540863941000,\"videoFiles\":{\"fileId\":20,\"moniid\":\"1000003\",\"fileName\":\"20180724 _20180724163102_162702.mp \",\"fileNameafterupload\":\"1540863941796.mp4\",\"filePathafterupload\":\"/home/vap/files/videoFile/10000\",\"createTime\":1540863942000,\"startTime\":1540863909000,\"fileSize\":131963967,\"videoFiles\":{\"fileId\":20,\"moniid\":\"1000003\",\"fileName\":\"20180724 _20180724163102_162702.mp \",\"fileNameafterupload\":\"1540863941796.mp4\",\"filePathafterupload\":\"/home/vap/files/videoFile/10000\",\"createTime\":1540863942000,\"startTime\":1540863909000,\"fileSize\":131963967}}}}"; JSONObject result = JSONObject.parseObject(responseStr); // 參數映射關系,沒有配置的則舍棄 String responseRelations = "[ {\"id\": 2184,\"newAtt\": \"fileNameafterupload3\",\"oldAtt\": \"fileNameafterupload\",\"attrType\": \"string\",\"parentId\": 2180}\n" + ", {\"id\": 2183,\"newAtt\": \"fileName3\",\"oldAtt\": \"fileName\",\"attrType\": \"string\",\"parentId\": 2180}\n" + ", {\"id\": 2182,\"newAtt\": \"moniid3\",\"oldAtt\": \"moniid\",\"attrType\": \"string\",\"parentId\": 2180}\n" + ", {\"id\": 2181,\"newAtt\": \"fileId3\",\"oldAtt\": \"fileId\",\"attrType\": \"string\",\"parentId\": 2180}\n" + ", {\"id\": 2180,\"newAtt\": \"videoFiles2\",\"oldAtt\": \"videoFiles\",\"attrType\": \"object\",\"parentId\": 2175}\n" + ", {\"id\": 2179,\"newAtt\": \"fileNameafterupload2\",\"oldAtt\": \"fileNameafterupload\",\"attrType\": \"string\",\"parentId\": 2175}\n" + ", {\"id\": 2178,\"newAtt\": \"fileName2\",\"oldAtt\": \"fileName\",\"attrType\": \"string\",\"parentId\": 2175}\n" + ", {\"id\": 2177,\"newAtt\": \"moniid2\",\"oldAtt\": \"moniid\",\"attrType\": \"string\",\"parentId\": 2175}\n" + ", {\"id\": 2176,\"newAtt\": \"fileId2\",\"oldAtt\": \"fileId\",\"attrType\": \"string\",\"parentId\": 2175}\n" + ", {\"id\": 2175,\"newAtt\": \"videoFiles1\",\"oldAtt\": \"videoFiles\",\"attrType\": \"object\",\"parentId\": 2169}\n" + ", {\"id\": 2174,\"newAtt\": \"locationName1\",\"oldAtt\": \"locationName\",\"attrType\": \"string\",\"parentId\": 2169}\n" + ", {\"id\": 2173,\"newAtt\": \"location1\",\"oldAtt\": \"location\",\"attrType\": \"string\",\"parentId\": 2169}\n" + ", {\"id\": 2172,\"newAtt\": \"moniname1\",\"oldAtt\": \"moniname\",\"attrType\": \"string\",\"parentId\": 2169}\n" + ", {\"id\": 2171,\"newAtt\": \"monitype1\",\"oldAtt\": \"monitype\",\"attrType\": \"string\",\"parentId\": 2169}\n" + ", {\"id\": 2170,\"newAtt\": \"moniid1\",\"oldAtt\": \"moniid\",\"attrType\": \"string\",\"parentId\": 2169}\n" + ", {\"id\": 2169,\"newAtt\": \"data\",\"oldAtt\": \"data\",\"attrType\": \"object\",\"parentId\": 0}\n" + ", {\"id\": 2168,\"newAtt\": \"count\",\"oldAtt\": \"count\",\"attrType\": \"object\",\"parentId\": 0}]"; List<ResponseCodeMsgRelation> responseCodeMsgRelations = JSONArray.parseArray(responseRelations,ResponseCodeMsgRelation.class); // 一級參數父Id默認為0 int parentAttId = 0; // 參數映射處理 JSONObject jsonObject = attrTurnBack(parentAttId, responseCodeMsgRelations, result); LOG.info("返回參數映射處理:" + jsonObject); } /** * @throws Exception * @Description: 參數轉換 * @Param: * @return: * @author: hw * @date: 2019/8/19 16:22 */ public static JSONObject attrTurnBack(int parentAttId, List<ResponseCodeMsgRelation> responseCodeMsgRelations, JSONObject jsonObj) { JSONObject resJson = new JSONObject(); try { Set<String> keySet = jsonObj.keySet(); int count = 0; for (String key : keySet) { String resKey = ""; int mineId = -1; for (int i = 0; i < responseCodeMsgRelations.size(); i++) { //映射對象 ResponseCodeMsgRelation responseCodeMsgRelation = responseCodeMsgRelations.get(i); if (responseCodeMsgRelation.getOldAtt().equals(key)) { //判斷父層是否一致 int parentId = responseCodeMsgRelation.getParentId(); if (parentAttId == parentId) { count++; mineId = responseCodeMsgRelation.getId(); resKey = responseCodeMsgRelation.getNewAtt(); break; } } } if (resKey != "" && resKey != null) { if (jsonObj.get(key) instanceof JSONObject) { JSONObject jsonobj1 = (JSONObject) jsonObj.get(key); resJson.put(resKey, attrTurnBack(mineId, responseCodeMsgRelations, jsonobj1)); } else if (jsonObj.get(key) instanceof JSONArray) { JSONArray jsonArr = (JSONArray) jsonObj.get(key); resJson.put(resKey, attrTurnBack(mineId, responseCodeMsgRelations, jsonArr)); } else { resJson.put(resKey, jsonObj.get(key)); } } } //如果count為0,說明該jsonObject下所有key都沒有映射,則直接返回 if (count == 0) { resJson = jsonObj; } } catch (Exception e) { e.printStackTrace(); } return resJson; } public static JSONArray attrTurnBack(int parentAtt, List<ResponseCodeMsgRelation> responseCodeMsgRelations, JSONArray jsonArr) { JSONArray resJson = new JSONArray(); for (int i = 0; i < jsonArr.size(); i++) { if(jsonArr.get(i) instanceof JSONObject){ JSONObject jsonObj = jsonArr.getJSONObject(i); resJson.add(attrTurnBack(parentAtt, responseCodeMsgRelations, jsonObj)); }else { resJson=jsonArr; } } return resJson; } /** * 參數映射關系類 */ static class ResponseCodeMsgRelation{ private Integer id; // 原始參數名稱 private String oldAtt; // 映射參數名稱 private String newAtt; // 參數類型 1 String 2 boolean 3 int 4 long 5 float 6 double 7 object 8 array private String attrType; // 父節點 0:無 private Integer parentId; public String getAttrType() { return attrType; } public void setAttrType(String attrType) { this.attrType = attrType; } public Integer getParentId() { return parentId; } public void setParentId(Integer parentId) { this.parentId = parentId; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getOldAtt() { return oldAtt; } public void setOldAtt(String oldAtt) { this.oldAtt = oldAtt; } public String getNewAtt() { return newAtt; } public void setNewAtt(String newAtt) { this.newAtt = newAtt; } } }