为什么会有这么奇葩的需求

因为数据库外键到处飞,增删查改又特别多,给前台的数据又要在table上显示,又得填充form,一会儿是嵌套,一会儿是平铺,发来的数据就各种乱七八糟。

平铺Map变为嵌套

要求

Jersey接收前台传来的Map(Json)大概是这样的:

{
  "staffByStaId.id": "1010",
  "staffByStaId.position": "退休人员",
  "staffByStaId.baseInfoByDeptId.keyCode": "110114",
  "staffByStaId.name": "黄光焕",
  "staffByStaId.baseInfoByDeptId.value": "退干人员",
  "id": "10",
  "privilege": "0",
  "staffByStaId.baseInfoByDeptId.id": "110114",
  "staffByStaId.baseInfoByDeptId.tableName": "部门",
  "email": "10@com.cn",
  "staffByStaId.idCard": "42010**********032"
}

需要转换为:

{
  "id": "10",
  "privilege": "0",
  "email": "10@com.cn",
  "staffByStaId": {
    "baseInfoByDeptId": {
      "keyCode": "110114",
      "id": "110114",
      "value": "退干人员",
      "tableName": "部门"
    },
    "idCard": "42010**********032",
    "name": "黄光焕",
    "id": "1010",
    "position": "退休人员"
  }
}

代码实现

public static Map<String, Object> nestMap(Map<String, Object> map) {
    Set<String> keys = new HashSet<>();
    keys.addAll(map.keySet());
    int size = 1;
    String flag = null;
    /*第一次遍历,取出最深层次的key*/
    for (String s : keys) {
        /*两次转义*/
        String[] temp = s.split("\\.");
        int dotNum = temp.length;
        if (size < dotNum) {
            size = dotNum;
            flag = s.substring(0, s.lastIndexOf("."));
        }
    }
    /*递归跳出*/
    if (flag == null) {
        return map;
    }
    Map<String, Object> subMap = new HashMap<>();
    /*第二次遍历,将最深层次变为map*/
    for (String v : keys) {
        if (v.startsWith(flag)) {
            subMap.put(v.substring(flag.length() + 1, v.length()), map.get(v));
            map.remove(v);
        }
    }
    /*将最深层次map放入*/
    map.put(flag, subMap);
    /*递归*/
    return nestMap(map);
}

大致就是递归,外加尾优化: )

字符串String转Map

要求

拦截器拦截前台发来的数据时,只能拦截到InputStream,里面的数据(转为String之后)大约是这样的:

{"id":"108","email":"108@com.cn","pwd":null,"privilege":"0","status":"1","staffByStaId":
{"id":"1108","name":"丁德智","idCard":"42010**********213","edu":null,"position":"行政人员","degree":null,"staSnm":null,"memo":null,"baseInfoByDeptId":
{"id":"110012","tableName":"部门","keyCode":"110012","value":"学工处"},"baseInfoByRankId":null}}

需要变成一个Map。

代码实现

public Map<String, Object> str2Map(String str) {
    String x = str.replaceAll("\\{\"", "{,\"").replaceAll("\\}", ",}");
    String[] temp = x.split(":|,");
    List list = new ArrayList();
    int i = 0;
    List<Integer> l = new ArrayList<>();
    for (String s : temp) {
        list.add(s.replace("\"", ""));
        if (s.equals("{")) {
            /*添加下标*/
            l.add(list.size() - 1);
        } else if (s.equals("}")) {
            /*找出最后一个{的下标*/
            int lastNum = l.get(l.size() - 1);
            /*切割*/
            List map = new ArrayList();

            map.addAll(list.subList(lastNum, list.size()));
            System.out.println(map);
            /*删除到上一个{之前*/
            while (list.size() > lastNum) {
                list.remove(lastNum);
            }
            list.add(list2Map(map));
            l.remove(l.size() - 1);
        }
    }
    Map<String, Object> ans = (Map<String, Object>) list.get(0);
    return ans;
}

public Map<String, Object> list2Map(List list) {
    Map<String, Object> ans = new HashMap<>();
    for (int i = 1; i < list.size() - 1; i += 2) {
        ans.put(list.get(i).toString(), list.get(i + 1));
    }
    return ans;
}

大致就是一个进栈出栈。