写一个转换工具遇到的 记录下 http://tool.liyongqiang.com/tool/findUrltoMap
//String 转json String str="{\"website\":\"tool.liyongqiang.com\"}"; JSONObject json = JSON.parseObject(str); System.out.println(json.toString()); //String 转map String str1="{key3=value3, key2={key3=value3, key2=value2, key1=value1},key1=value1}"; Map map = (Map<String, Object>) getValue(str1); System.out.println(map.toString()); //map 转 json JSONObject json1 = JSONObject.parseObject(JSON.toJSONString(map));; System.out.println(json1.toString()); //json 转 map String str2 = "{\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"}"; Map mapType = JSON.parseObject(str2,Map.class); System.out.println(mapType.toString());
public static Object getValue(String param) { Map map = new HashMap(); String str = ""; String key = ""; Object value = ""; char[] charList = param.toCharArray(); boolean valueBegin = false; for (int i = 0; i < charList.length; i++) { char c = charList[i]; if (c == '{') { if (valueBegin == true) { value = getValue(param.substring(i, param.length())); i = param.indexOf('}', i) + 1; map.put(key, value); } } else if (c == '=') { valueBegin = true; key = str; str = ""; } else if (c == ',') { valueBegin = false; value = str; str = ""; map.put(key, value); } else if (c == '}') { if (str != "") { value = str; } map.put(key, value); return map; } else if (c != ' ') { str += c; } } return map; }