我们有一组原始数据,通过原始数据转化成为固定想要的数据;
原始数据: {data=[{类别=电网基建, 金额=2208.8}, {类别=生成科技, 金额=55.1}], name=sheet1} 转换后的数据: {"sheets":[{"datas":[{"cells":[{"dataType":2,"value":"电网基建"},{"dataType":1,"value":"2208.8"}]},{"cells":[{"dataType":2,"value":"生成科技"},{"dataType":1,"value":"55.1"}]}],"name":"sheet1","titles":["类别","金额"]}]}
DEMO1
package com.example.democrud.democurd.sqldemo; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.parser.Feature; import java.util.*; public class Demo02 { static String regStr = "^[+-]?[1-9][0-9]*$|^0$"; public static void main(String[] args) { String sql = "{\"data\":[{\"类别\": '电网基建', \"金额\": \"2208.8\"},{\"类别\":'生成科技', \"金额\": \"55.1\"}],\"name\":\"sheet1\"}"; JSONObject jsonObject = JSONObject.parseObject(sql,Feature.OrderedField); JSONArray jsonArray = JSONArray.parseArray(jsonObject.get("data").toString()); Set<String> titleSet = new HashSet<>(); List<JSONObject> cells = new ArrayList<>(); for (Object o : jsonArray) { if(Objects.isNull(o)){ continue; } JSONObject json = JSONObject.parseObject(o.toString(),Feature.OrderedField); for (Map.Entry<String, Object> stringObjectEntry : json.entrySet()) { titleSet.add(stringObjectEntry.getKey()); String value = stringObjectEntry.getValue().toString(); JSONObject sing = new JSONObject(); sing.put("value", value); if(value.matches(regStr)) { sing.put("dataType", 1); }else{ sing.put("dataType", 2); } cells.add(sing); } } JSONObject cellsJson = new JSONObject(); cellsJson.put("cells", cells); cellsJson.put("name", jsonObject.get("name")); cellsJson.put("titles", titleSet); JSONArray sheetsArray = new JSONArray(); sheetsArray.add(cellsJson); JSONObject result = new JSONObject(); result.put("sheets", sheetsArray); System.out.println("原始数据:" + jsonObject); System.out.println("修改后数据:" + result); } }
原始数据:{"data":[{"类别":"电网基建","金额":"2208.8"},{"类别":"生成科技","金额":"55.1"}],"name":"sheet1"} 修改后数据:{"sheets":[{"cells":[{"dataType":2,"value":"2208.8"},{"dataType":2,"value":"电网基建"},{"dataType":2,"value":"55.1"},{"dataType":2,"value":"生成科技"}],"name":"sheet1","titles":["金额","类别"]}]}
上面那个方式基本都是正常的,只是有些许问题;
DEMO1 优化
package com.example.democrud.democurd.sqldemo; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.parser.Feature; import java.util.*; public class Demo02 { static String regStr = "^[+-]?[1-9][0-9]*$|^0$"; public static void main(String[] args) { String sql = "{\"data\":[{\"类别\": '电网基建', \"金额\": \"2208.8\"},{\"类别\":'生成科技', \"金额\": \"55.1\"}],\"name\":\"sheet1\"}"; JSONObject jsonObject = JSONObject.parseObject(sql); JSONArray jsonArray = JSONArray.parseArray(jsonObject.get("data").toString()); Set<String> titleSet = new HashSet<>(); List<JSONObject> cells = new ArrayList<>(); for (Object o : jsonArray) { if(Objects.isNull(o)){ continue; } JSONObject json = JSONObject.parseObject(o.toString()); JSONObject jsonObject1 = new JSONObject(); JSONArray jsonArray1 = new JSONArray(); for (Map.Entry<String, Object> stringObjectEntry : json.entrySet()) { titleSet.add(stringObjectEntry.getKey()); String value = stringObjectEntry.getValue().toString(); JSONObject sing = new JSONObject(); if(value.matches(regStr)) { sing.put("dataType", 1); }else{ sing.put("dataType", 2); } sing.put("value", value); jsonArray1.add(sing); } jsonObject1.put("cells", jsonArray1); jsonArray1.sort(Comparator.comparing(obj -> ((JSONObject) obj).getString("value").length())); cells.add(jsonObject1); } JSONObject cellsJson = new JSONObject(); cellsJson.put("data", cells); cellsJson.put("name", jsonObject.get("name")); cellsJson.put("titles", titleSet); JSONArray sheetsArray = new JSONArray(); sheetsArray.add(cellsJson); JSONObject result = new JSONObject(); result.put("sheets", sheetsArray); System.out.println("原始数据:" + jsonObject); System.out.println("修改后数据:" + result); } }
原始数据:{"data":[{"金额":"2208.8","类别":"电网基建"},{"金额":"55.1","类别":"生成科技"}],"name":"sheet1"} 修改后数据:{"sheets":[{"data":[{"cells":[{"dataType":2,"value":"电网基建"},{"dataType":2,"value":"2208.8"}]},{"cells":[{"dataType":2,"value":"55.1"},{"dataType":2,"value":"生成科技"}]}],"name":"sheet1","titles":["金额","类别"]}]}
DEMO2
package com.ruoyi.web.controller.tool; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.util.*; public class demo03 { static String regStr = "^[+-]?([1-9][0-9]*|0)(\\.[0-9]+)?$"; public static void main(String[] args) { String sql = "{\"data\":[{\"类别\": '电网基建', \"金额\": \"2208.8\"},{\"类别\":'生成科技', \"金额\": \"55.1\"}],\"name\":\"sheet1\"}"; Map map = json2Map(sql); System.out.println(map.toString()); JSONObject sheets = new JSONObject(){{ put("sheets",new JSONArray(){{ add(new JSONObject(){{ put("datas",new JSONArray(){{ ((List<Map>)map.get("data")).forEach(d->{ add(new JSONObject(){{ put("cells",new JSONArray(){{ d.values().forEach(e->{ add(new JSONObject(){{ if (e.toString().matches(regStr)){ put("dataType",1); }else { put("dataType",2); } put("value",e); }}); }); }}); }}); }); }}); put("name",map.get("name")); put("titles",((List<Map>)map.get("data")).get(0).keySet()); }}); }}); }}; System.out.println(sheets); } public static Map<String, Object> json2Map(String jsonStr){ Map<String, Object> map = new TreeMap<>(); JSONObject json = JSON.parseObject(jsonStr); if (jsonStr != null||!"".equals(jsonStr)){ for (Object k : json.keySet()){ Object v = json.get(k); if (v instanceof JSONArray){ List<Map<String, Object>> list = new ArrayList<>(); Iterator<Object> it = ((JSONArray)v).iterator(); while (it.hasNext()){ list.add(json2Map(it.next().toString())); } map.put(k.toString(), list); }else if(v instanceof JSONObject){ map.put(k.toString(), json2Map(v.toString())); }else { map.put(k.toString(), v); } } return map; }else { return null; } } }
{data=[{类别=电网基建, 金额=2208.8}, {类别=生成科技, 金额=55.1}], name=sheet1} {"sheets":[{"datas":[{"cells":[{"dataType":2,"value":"电网基建"},{"dataType":1,"value":"2208.8"}]},{"cells":[{"dataType":2,"value":"生成科技"},{"dataType":1,"value":"55.1"}]}],"name":"sheet1","titles":["类别","金额"]}]}
Demo3
有点乱不太推荐
package com.example.democrud.democurd.sqldemo; import com.alibaba.fastjson.JSON; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.lang3.StringUtils; import java.util.*; public class demo01 { public static void main(String[] args) { // String sql = "{data:[{id: 242124591104, fileName: \"接口文档-1675230120230.pdf\", filePath: \"/file/SECOND/\", fileType: \"1\"}],[{id: 24218888888, fileName: \"接口文档-167588888.pdf\", filePath: \"/file/SECOND/\", fileType: \"8\"}],\"name\":\"sheet1\"}"; String sql = "{\"data\":[{\"类别\": '电网基建', \"金额\": \"2208.8\"},{\"类别\":'生成科技', \"金额\": \"55.1\"}],\"name\":\"sheet1\"}"; user user3 = new user(); JSONObject json = JSONObject.fromObject(sql); if (StringUtils.isNoneBlank(json.get("name").toString())){ user3.setName(json.get("name").toString()); } // Map<String, Object> map = json2Map(sql); Map<String, Object> map = json2Map(sql); System.out.println("------------------------------"); Object map1 = map.get("data"); System.out.println(map1); HashMap<Object, Object> map4 = new HashMap<>(); List<Object> list1 = new ArrayList<>(); List<Object> list2 = new ArrayList<>(); List<Object> list3 = new ArrayList<>(); List<Object> list4 = new ArrayList<>(); for (Map.Entry<String, Object> entry : map.entrySet()) { String key = entry.getKey(); System.out.println("key===>" + key); Object list = entry.getValue(); System.out.println("list===>" + list); JSONArray aa = JSONArray.fromObject(list); System.out.println("aa===>" + aa); for (Object map2 : aa) { String regStr = "^[+-]?[1-9][0-9]*$|^0$"; int te; JSONObject map3 = JSONObject.fromObject(map2); String values; for (Object entry1 : map3.entrySet()) { String keys= (String) ((Map.Entry)entry1).getKey(); values = (String) ((Map.Entry)entry1).getValue(); System.out.println("key的值==>"+keys); System.out.println("value的值==>"+values); if(values.matches(regStr)) { te=1; }else{ te=2; } user1 user1 = new user1(te,values); String str1 = JSON.toJSONString(user1); list1.add(str1); System.out.println(list1); list3.add(keys); } } list3.forEach(i -> { if (!list4.contains(i)) { // 如果新集合中不存在则插入 list4.add(i); } }); list2.addAll(list1); map4.put("cells",list2); map4.put("titles",list4); map4.put("name",user3.getName()); System.out.println(map4); HashMap<Object, Object> map5 = new HashMap<>(); map5.put("sheets",map4); System.out.println(map5); } } public static Map<String, Object> json2Map(String sql) { Map<String, Object> map = new HashMap<>(); HashMap<String, Object> hashMap = new HashMap<>(); if (sql != null && !"".equals(sql)) { //转换为json JSONObject json = JSONObject.fromObject(sql); System.out.println("json的==》"+json); for (Object k : json.keySet()) { Object v = json.get(k); System.out.println("获取的k==>" + k + ":获取的v==>" + v); map.put(k.toString(), v); /* if (v instanceof JSONArray) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Iterator iterator = ((JSONArray) v).iterator(); while (iterator.hasNext()) { Object next = iterator.next(); list.add(json2Map(next.toString())); } map.put(k.toString(), list); } else { map.put(k.toString(), v); }*/ System.out.println("map的值---1------------------"+map); return map; } System.out.println("map的值---2------------------"+map); return map; }else { return null; } } @Data @NoArgsConstructor @AllArgsConstructor public static class user1 { //k的值的类型 public int dataType; //v的值 public String value; } public static class user{ //k的值的类型 public String dataType; //v的值 public String value; //表的值 表名 public String name; //k的值 数据方式展示 public byte titles[]; //集合 包住值的类型和值 public List<Object> cells; public String getDataType() { return dataType; } public void setDataType(String dataType) { this.dataType = dataType; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getName() { return name; } public void setName(String name) { this.name = name; } public byte[] getTitles() { return titles; } public void setTitles(byte[] titles) { this.titles = titles; } public List<Object> getCells() { return cells; } public void setCells(List<Object> cells) { this.cells = cells; } } }
json的==》{"data":[{"类别":"电网基建","金额":"2208.8"},{"类别":"生成科技","金额":"55.1"}],"name":"sheet1"} 获取的k==>data:获取的v==>[{"类别":"电网基建","金额":"2208.8"},{"类别":"生成科技","金额":"55.1"}] map的值---1------------------{data=[{"类别":"电网基建","金额":"2208.8"},{"类别":"生成科技","金额":"55.1"}]} ------------------------------ [{"类别":"电网基建","金额":"2208.8"},{"类别":"生成科技","金额":"55.1"}] key===>data list===>[{"类别":"电网基建","金额":"2208.8"},{"类别":"生成科技","金额":"55.1"}] aa===>[{"类别":"电网基建","金额":"2208.8"},{"类别":"生成科技","金额":"55.1"}] key的值==>类别 value的值==>电网基建 [{"dataType":2,"value":"电网基建"}] key的值==>金额 value的值==>2208.8 [{"dataType":2,"value":"电网基建"}, {"dataType":2,"value":"2208.8"}] key的值==>类别 value的值==>生成科技 [{"dataType":2,"value":"电网基建"}, {"dataType":2,"value":"2208.8"}, {"dataType":2,"value":"生成科技"}] key的值==>金额 value的值==>55.1 [{"dataType":2,"value":"电网基建"}, {"dataType":2,"value":"2208.8"}, {"dataType":2,"value":"生成科技"}, {"dataType":2,"value":"55.1"}] {cells=[{"dataType":2,"value":"电网基建"}, {"dataType":2,"value":"2208.8"}, {"dataType":2,"value":"生成科技"}, {"dataType":2,"value":"55.1"}], name=sheet1, titles=[类别, 金额]} {sheets={cells=[{"dataType":2,"value":"电网基建"}, {"dataType":2,"value":"2208.8"}, {"dataType":2,"value":"生成科技"}, {"dataType":2,"value":"55.1"}], name=sheet1, titles=[类别, 金额]}}
递归小demo(上面用到了 简单理解下)
我的理解 :(不对别骂我)
递归 是不是就是 执行 递归循环(也就是符合跳出条件)跳出循环之后先输出跳出的循环的值 然后依次倒序输出 中间的过程
这个文章写的不错大家可以才看下;
package com.example.democrud.democurd.sqldemo; import lombok.Data; import java.util.Date; public class demo5 { public static int test(int i){ System.out.println("这是"+i); return i+1; } public static void test2(int i){ if (i>2){ System.out.println("i=="+i); }else { System.out.println("i="+i+"进入递归"); test2(i+1); System.out.println("i="+i+"递归屁股"); } } public static void main(String[] args) { // methodTest(9); // test(test(test(1))); /* test2的理解: 当i为1的时候,进入方法体,i=1>2 不满足进入else递归中,i=1+1=2 此时的i=2 他也不大于2 故再次else 2+1=3; 3>2 故进入 if方法中输出if中的sout; 其次 这块的我的理解 就是倒序 输入前面的2个值 */ // test2(1); test3(5); } public static void test3(int i){ if (i<1){ System.out.println(i); }else{ System.out.println("开始"+i); test3(i-2); System.out.println("中间"+i); // test(i*2-1); // System.out.println("结尾"+i); } } /* * @Author LY * @Description 递归方式实现九九乘法表 * @Date 22:21 2020/8/13 **/ private static void methodTest(int i) { if (i == 1) { //递归头:控制什么时候结束调用自己 System.out.println("1*1=1 "); } else { //递归体:什么时候调用自己 methodTest(i - 1); for (int j = 1; j <= i; j++) { System.out.print(j + "*" + i + "=" + j * i + "\t"); } System.out.println(); } } }