JSONObject参数递归转换
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;
}
}
}
==========================================================================================
我希望每一篇文章的背后,都能看到自己对于技术、对于生活的态度。
我相信乔布斯说的,只有那些疯狂到认为自己可以改变世界的人才能真正地改变世界。面对压力,我可以挑灯夜战、不眠不休;面对困难,我愿意迎难而上、永不退缩。
其实我想说的是,我只是一个程序员,这就是我现在纯粹人生的全部。
==========================================================================================

浙公网安备 33010602011771号