前端传参对象套对象的格式,后端进行解析并存入数据库

项目场景:

在一些小程序中会涉及到一个表单中的一个数组对象中夹杂着另一个对象数据,这个被夹杂的数据是from表单内的一组单选或者双选这样的数据,前端转为json对象之后传到后端进行解析


问题描述

首先传进来的对象是这样的格式的

abc:{"a":"0","a":"1","a":"0","a":"0","a":"1","a":"0",abcd={"a":"0","a":"0","a":"0","a":"0"},"a":"0"}

他是对象套对象的格式,有两个需求第一个是内部里面的这个对象需要取出来根据指定名称进行拼接存入一个字段然后和整个对象一起存入到第一个a表内,然后遍历第二个对象根据名称进行业务处理之后依次存入第二个b表内


解决方案:

我采取的时迭代器和for循环实现的,迭代器晒出内部对象,然后放入map内,for循环遍历map进行业务处理,下面是具体的实现步骤

这是简化代码,下面是我具体的业务处理,用的都是比较常见的方法

JSONObject jsonInfo = new JSONObject();
jsonInfo.put("add", houseSafetyContents);

Map> map = new TreeMap<>(); // 使用TreeMap来存储键值对,保证按照键的自然顺序遍历
Iterator keys = jsonInfo.keys();
while (keys.hasNext()) {
  String key = keys.next();
  JSONObject value = jsonInfo.getJSONObject(key);
  Map innerMap = new HashMap<>();
  
  Iterator innerKeys = value.keys();
  while (innerKeys.hasNext()) {
    String innerKey = innerKeys.next();
    String innerValue = value.getString(innerKey);
    System.out.println(innerValue);
    System.out.println(innerKey);
    innerMap.put(innerKey, innerValue);
  }
  
  map.put(key, innerMap);
}

// 按照键的自然顺序遍历键值对
for (Map.Entry> entry : map.entrySet()) {
  String key = entry.getKey();
  Map innerMap = entry.getValue();
  System.out.println("Key: " + key);
  for (Map.Entry innerEntry : innerMap.entrySet()) {
    String innerKey = innerEntry.getKey();
    String innerValue = innerEntry.getValue();
    System.out.println(innerKey + " - " + innerValue);
  }
}
 

public List insertSecurityCheck(QdmHousesaFetyVO qdmHousesaFetyVO) {
        QdmHousesaFetyinfoVO qdmHousesaFetyinfoVO = new QdmHousesaFetyinfoVO();
        //定义拼接安检内容的字段
        String contents = null;
        String contents1 = null;
        String contents8 = null;
        String contents9 = null;
        String contents10 = null;
        String contents11 = null;
        String contents13 = null;
        String contents14 = null;
        String contents15 = null;
        String contents16 = null;
        String contents17 = null;
        String contents18 = null;

        JSONObject jsonInfo = new JSONObject();
        //将安检内容转为字符串
        String jsonString = qdmHousesaFetyVO.getHouseSafetyContents().toString();
        //将抓换字符串的安检内容转为json
        JSONObject houseSafetyContents = new JSONObject(jsonString);
        //传入到json对象准备遍历
        jsonInfo.put("add", houseSafetyContents);

        Map> map = new TreeMap<>(); // 使用TreeMap来存储键值对,保证按照键的自然顺序遍历 存放存入安检表的map
        Map> map1 = new TreeMap<>(); // 使用TreeMap来存储键值对,保证按照键的自然顺序遍历  存放全部安检的map
        //获取里面的key
        Iterator keys = jsonInfo.keys();
        //定义迭代器 遍历这个json对象 获取key并赋值,然后通过这个赋值的key得到对应值
        while (keys.hasNext()) {
            String key = keys.next();
            JSONObject value = jsonInfo.getJSONObject(key);
            Map innerMap = new HashMap<>();
            Iterator innerKeys = value.keys();
            //定义第二个迭代器 遍历这个内层的value里面的key和value,然后将key对应的value赋值
            while (innerKeys.hasNext()) {
                String innerKey = innerKeys.next();
                String innerValue = value.getString(innerKey);
                innerMap.put(innerKey, innerValue);
            }

            map.put(key, innerMap);
            map1.put(key, innerMap);
        }
// 打印结果
        //第一个循环 获取外层的map 得到内层的数据在存入到map
        for (Map.Entry> entry : map1.entrySet()) {
            String key = entry.getKey();
            Map innerMap = entry.getValue();
            //第二个循环 遍历内部的map 获得想要的数据
            for (Map.Entry innerEntry : innerMap.entrySet()) {
                String innerKey = innerEntry.getKey();
                String innerValue = innerEntry.getValue();
                if (innerKey.equals("fuel_1")) {
                    qdmHousesaFetyinfoVO.setContents("是否在开放式厨房使用燃气");
                    if (innerValue.equals("1")) {
                        //1是合格0不为合格
                        qdmHousesaFetyinfoVO.setIsConformity("是");
                    }
                    if (innerValue.equals("0")) {
                        qdmHousesaFetyinfoVO.setIsConformity("否");
                    }
                    contents = qdmHousesaFetyinfoVO.getContents() + "" + qdmHousesaFetyinfoVO.getIsConformity();
                }
                if (innerKey.equals("fuel_2")) {
                    qdmHousesaFetyinfoVO.setContents("是否存在双气源、双火源");
                    if (innerValue.equals("1")) {
                        //1是合格0不为合格
                        qdmHousesaFetyinfoVO.setIsConformity("是");
                    }
                    if (innerValue.equals("0")) {
                        //1是不合格0为合格
                        qdmHousesaFetyinfoVO.setIsConformity("否");
                    }
                    contents1 = qdmHousesaFetyinfoVO.getContents() + "" + qdmHousesaFetyinfoVO.getIsConformity();
                }
                if (innerKey.equals("siren_1")) {
                    qdmHousesaFetyinfoVO.setContents("是否具备燃气安全技防装置(报警器与切断连锁装置或自闭阀)");
                    if (innerValue.equals("1")) {
                        //1是合格0不为合格
                        qdmHousesaFetyinfoVO.setIsConformity("是");
                    }
                    if (innerValue.equals("0")) {
                        //1是不合格0为合格
                        qdmHousesaFetyinfoVO.setIsConformity("否");
                    }
                    contents8 = qdmHousesaFetyinfoVO.getContents() + "" + qdmHousesaFetyinfoVO.getIsConformity();
                }
                if (innerKey.equals("siren_2")) {
                    System.out.println("");
                    qdmHousesaFetyinfoVO.setContents("报警器是否在工作状态");
                    if (innerValue.equals("1")) {
                        //1是合格0不为合格
                        qdmHousesaFetyinfoVO.setIsConformity("是");
                    }
                    if (innerValue.equals("0")) {
                        //1是不合格0为合格
                        qdmHousesaFetyinfoVO.setIsConformity("否");
                    }
                    contents9 = qdmHousesaFetyinfoVO.getContents() + "" + qdmHousesaFetyinfoVO.getIsConformity();
                }
               
                
                if (innerKey.equals("link_1")) {
                    System.out.println("");
                    qdmHousesaFetyinfoVO.setContents("是否使用金属波纹连接管");
                    if (innerValue.equals("1")) {
                        //1是合格0不为合格
                        qdmHousesaFetyinfoVO.setIsConformity("是");
                    }
                    if (innerValue.equals("0")) {
                        //1是不合格0为合格
                        qdmHousesaFetyinfoVO.setIsConformity("否");
                    }
                    contents13 = qdmHousesaFetyinfoVO.getContents() + "" + qdmHousesaFetyinfoVO.getIsConformity();
                }
                if (innerKey.equals("link_2")) {
                    System.out.println("");
                    qdmHousesaFetyinfoVO.setContents("连接管是否超长(大于2m)");
                    if (innerValue.equals("1")) {
                        //1是合格0不为合格
                        qdmHousesaFetyinfoVO.setIsConformity("是");
                    }
                    if (innerValue.equals("0")) {
                        //1是不合格0为合格
                        qdmHousesaFetyinfoVO.setIsConformity("否");
                    }
                    contents14 = qdmHousesaFetyinfoVO.getContents() + "" + qdmHousesaFetyinfoVO.getIsConformity();
                }
               
                    contents18 = qdmHousesaFetyinfoVO.getContents() + "" + qdmHousesaFetyinfoVO.getIsConformity();
                }

//                System.out.println(innerKey + " - " + innerValue);

            }


        }
        qdmHousesaFetyVO.setHouseSafetyContents(contents+";"+contents1+";"+contents8+";"+contents9+";"+contents10+";"+contents11+";"+contents13+";"+contents14+";"+contents15+";"+contents16+";"+contents17+";"+contents18);

        qdmSecCheMapper.insertCustomerDetails(qdmHousesaFetyVO);
        int ID =  qdmSecCheMapper.selectID();
//        System.out.println("selectID: "+ID);
        for (Map.Entry> entry : map.entrySet()) {
            String key = entry.getKey();
            Map innerMap = entry.getValue();
//            System.out.println("Key: " + key);
            for (Map.Entry innerEntry : innerMap.entrySet()) {
                String innerKey = innerEntry.getKey();
                String innerValue = innerEntry.getValue();
                switch(innerKey) {
                    case "fuel_1" :  qdmHousesaFetyinfoVO.setModule("燃气使用环境");
                        qdmHousesaFetyinfoVO.setContents("是否在开放式厨房使用燃气");
                        if (innerValue.equals("1")){
                            //1是不合格0为合格
                            qdmHousesaFetyinfoVO.setCheckResult("是");
                            qdmHousesaFetyinfoVO.setIsConformity("合格");
                            qdmHousesaFetyinfoVO.setIsConformityInt("1");
                        }if (innerValue.equals("0")){
                        //1是不合格0为合格
                        qdmHousesaFetyinfoVO.setCheckResult("否");
                        qdmHousesaFetyinfoVO.setIsConformity("不合格");
                        qdmHousesaFetyinfoVO.setIsConformityInt("0");
                    }
                        qdmHousesaFetyinfoVO.setInfoID(ID);
                        qdmSecCheMapper.inserthousesafetyinfo(qdmHousesaFetyinfoVO);break;
                    case "fuel_2" :  qdmHousesaFetyinfoVO.setModule("燃气使用环境");
                        qdmHousesaFetyinfoVO.setContents("是否存在双气源、双火源");
                        if (innerValue.equals("1")){
                            //1是不合格0为合格
                            qdmHousesaFetyinfoVO.setCheckResult("是");
                            qdmHousesaFetyinfoVO.setIsConformity("合格");
                            qdmHousesaFetyinfoVO.setIsConformityInt("1");
                        }if (innerValue.equals("0")){
                        //1是不合格0为合格
                        qdmHousesaFetyinfoVO.setCheckResult("否");
                        qdmHousesaFetyinfoVO.setIsConformity("不合格");
                        qdmHousesaFetyinfoVO.setIsConformityInt("0");
                    }
                        qdmHousesaFetyinfoVO.setInfoID(ID);
                        qdmSecCheMapper.inserthousesafetyinfo(qdmHousesaFetyinfoVO);break;
                    case "siren_1" :
                        qdmHousesaFetyinfoVO.setModule("报警器及与其联动的切断装置");
                        qdmHousesaFetyinfoVO.setContents("是否具备燃气安全技防装置(报警器与切断连锁装置或自闭阀)");
                        if (innerValue.equals("1")){
                            //1是合格0为不合格
                            qdmHousesaFetyinfoVO.setCheckResult("是");
                            qdmHousesaFetyinfoVO.setIsConformity("合格");
                            qdmHousesaFetyinfoVO.setIsConformityInt("1");
                        }if (innerValue.equals("0")){
                        //1是合格0为不合格
                        qdmHousesaFetyinfoVO.setCheckResult("否");
                        qdmHousesaFetyinfoVO.setIsConformity("不合格");
                        qdmHousesaFetyinfoVO.setIsConformityInt("0");
                    }
                        qdmHousesaFetyinfoVO.setInfoID(ID);
                        qdmSecCheMapper.inserthousesafetyinfo(qdmHousesaFetyinfoVO);break;
                    case "siren_2" : qdmHousesaFetyinfoVO.setModule("报警器及与其联动的切断装置");
                        qdmHousesaFetyinfoVO.setContents("报警器是否在工作状态");
                        if (innerValue.equals("1")){
                            //1是合格0为不合格
                            qdmHousesaFetyinfoVO.setCheckResult("是");
                            qdmHousesaFetyinfoVO.setIsConformity("合格");
                            qdmHousesaFetyinfoVO.setIsConformityInt("1");
                        }if (innerValue.equals("0")){
                        //1是合格0为不合格
                        qdmHousesaFetyinfoVO.setCheckResult("否");
                        qdmHousesaFetyinfoVO.setIsConformity("不合格");
                        qdmHousesaFetyinfoVO.setIsConformityInt("0");
                    }
                        qdmHousesaFetyinfoVO.setInfoID(ID);
                        qdmSecCheMapper.inserthousesafetyinfo(qdmHousesaFetyinfoVO);break;
                    
                    case "link_1" : qdmHousesaFetyinfoVO.setModule("连接软管与灶具");
                        qdmHousesaFetyinfoVO.setContents("是否使用金属波纹连接管");
                        if (innerValue.equals("1")){
                            //1是合格0为不合格
                            qdmHousesaFetyinfoVO.setCheckResult("是");
                            qdmHousesaFetyinfoVO.setIsConformity("合格");
                            qdmHousesaFetyinfoVO.setIsConformityInt("1");
                        }if (innerValue.equals("0")){
                        //1是合格0为不合格
                        qdmHousesaFetyinfoVO.setCheckResult("否");
                        qdmHousesaFetyinfoVO.setIsConformity("不合格");
                        qdmHousesaFetyinfoVO.setIsConformityInt("0");
                    }
                        qdmHousesaFetyinfoVO.setInfoID(ID);
                        qdmSecCheMapper.inserthousesafetyinfo(qdmHousesaFetyinfoVO);break;
                    case "link_2" : qdmHousesaFetyinfoVO.setModule("连接软管与灶具");
                        qdmHousesaFetyinfoVO.setContents("连接管是否超长(大于2m)");
                        if (innerValue.equals("1")){
                            //0是合格1为不合格
                            qdmHousesaFetyinfoVO.setCheckResult("是");
                            qdmHousesaFetyinfoVO.setIsConformity("合格");
                            qdmHousesaFetyinfoVO.setIsConformityInt("1");
                        }if (innerValue.equals("0")){
                        //0是合格1为不合格
                        qdmHousesaFetyinfoVO.setCheckResult("否");
                        qdmHousesaFetyinfoVO.setIsConformity("不合格");
                        qdmHousesaFetyinfoVO.setIsConformityInt("0");
                    }
                        qdmHousesaFetyinfoVO.setInfoID(ID);
                        qdmSecCheMapper.inserthousesafetyinfo(qdmHousesaFetyinfoVO);break;
                    default : System.out.println(0);
                }
            }

        }
        List Housesa = qdmSecCheMapper.selectHousesa(ID);
        String qualified = "1";
        for (int i = 0; i < Housesa.size(); i++) {
            QdmHousesaFetyinfoVO house = Housesa.get(i);
            String IsConformityInt = house.getIsConformityInt();
            String IsConformity = house.getIsConformity();
            if (IsConformityInt.equals("0") || IsConformity.equals("不合格")) {
                qualified = "0";
                break;
            }
        }
        qdmSecCheMapper.updateYhqhousesafety(ID, qualified);

        List data =  qdmSecCheMapper.selecthousesafetyinfo();
        System.out.println(data);
        return data;
    }

你可能感兴趣的:(前端,数据库,java)