• 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}
    " +
                    ", {"id": 2183,"newAtt": "fileName3","oldAtt": "fileName","attrType": "string","parentId": 2180}
    " +
                    ", {"id": 2182,"newAtt": "moniid3","oldAtt": "moniid","attrType": "string","parentId": 2180}
    " +
                    ", {"id": 2181,"newAtt": "fileId3","oldAtt": "fileId","attrType": "string","parentId": 2180}
    " +
                    ", {"id": 2180,"newAtt": "videoFiles2","oldAtt": "videoFiles","attrType": "object","parentId": 2175}
    " +
                    ", {"id": 2179,"newAtt": "fileNameafterupload2","oldAtt": "fileNameafterupload","attrType": "string","parentId": 2175}
    " +
                    ", {"id": 2178,"newAtt": "fileName2","oldAtt": "fileName","attrType": "string","parentId": 2175}
    " +
                    ", {"id": 2177,"newAtt": "moniid2","oldAtt": "moniid","attrType": "string","parentId": 2175}
    " +
                    ", {"id": 2176,"newAtt": "fileId2","oldAtt": "fileId","attrType": "string","parentId": 2175}
    " +
                    ", {"id": 2175,"newAtt": "videoFiles1","oldAtt": "videoFiles","attrType": "object","parentId": 2169}
    " +
                    ", {"id": 2174,"newAtt": "locationName1","oldAtt": "locationName","attrType": "string","parentId": 2169}
    " +
                    ", {"id": 2173,"newAtt": "location1","oldAtt": "location","attrType": "string","parentId": 2169}
    " +
                    ", {"id": 2172,"newAtt": "moniname1","oldAtt": "moniname","attrType": "string","parentId": 2169}
    " +
                    ", {"id": 2171,"newAtt": "monitype1","oldAtt": "monitype","attrType": "string","parentId": 2169}
    " +
                    ", {"id": 2170,"newAtt": "moniid1","oldAtt": "moniid","attrType": "string","parentId": 2169}
    " +
                    ", {"id": 2169,"newAtt": "data","oldAtt": "data","attrType": "object","parentId": 0}
    " +
                    ", {"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;
            }
        }
    }
    

      

  • 相关阅读:
    ReactiveCocoa RACObserve subscribeNext 时,只有值不一样时才响应
    ReactiveCocoa 监听Enabled和添加Command出错的处理方法
    Masonry + UIView Animations 注意事项
    addObserver forKeyPath options 注意事项
    ios中tabbar得title和navigationbar的title如何修改
    tableview 分组显示返回footerviewt和headerView的高度不能为0的问题
    UITableViewCell的选中时的颜色设置
    ios 枚举 位移操作
    设置UIButton 字体 颜色
    jsoup 源码分析
  • 原文地址:https://www.cnblogs.com/weihuang6620/p/14073799.html
Copyright © 2020-2023  润新知