当前位置: 代码迷 >> 综合 >> 手写JSON与集合互转
  详细解决方案

手写JSON与集合互转

热度:12   发布时间:2024-01-17 22:36:10.0

List、Map、JSON、String 互转方法总结:

先叙述一下没有jar包时候的困惑;

不行就自己写一个吧!

public static List parseStringToListMap(String str1) {if (StringUtil.isBlank(str1)){return new ArrayList();}String[] oraginStr = str1.split("}, \\{");List resultList = Arrays.asList(oraginStr);if(oraginStr.length<=1){return resultList;}List listRest = new ArrayList();for (int j = 0; j < resultList.size(); j++) {String checkIndex = String.valueOf(resultList.get(j));checkIndex = checkIndex.replaceAll("]", "").replaceAll("\\[", "").replaceAll("\\{", "").replaceAll("}", "");String str2[] = checkIndex.split(",");Map mapIn = new HashMap();for (int i = 0; i < str2.length; i++) {String str3 = str2[i];if (StringUtil.isBlank(str3)) {continue;}String str4[] = str3.split("=");if (2 == str4.length) {mapIn.put(str4[0].trim(), str4[1].trim());} else if (1 == str4.length) {mapIn.put(str4[0].trim(), "");} else {continue;}}listRest.add(mapIn);}return listRest;}