String转json,转map,json与map互转
时间:2021-11-10 作者:匿名
写一个转换工具遇到的 记录下 http://域名/tool/findUrltoMap
//String 转json String str="{\\"website\\":\\"域名\\"}"; JSONObject json = 域名eObject(str); 域名tln(域名ring()); //String 转map String str1="{key3=value3, key2={key3=value3, key2=value2, key1=value1},key1=value1}"; Map map = (Map<String, Object>) getValue(str1); 域名tln(域名ring()); //map 转 json JSONObject json1 = 域名eObject(域名ONString(map));; 域名tln(域名ring()); //json 转 map String str2 = "{\\"0\\":\\"zhangsan\\",\\"1\\":\\"lisi\\",\\"2\\":\\"wangwu\\",\\"3\\":\\"maliu\\"}"; Map mapType = 域名eObject(str2,域名s); 域名tln(域名ring());
public static Object getValue(String param) { Map map = new HashMap(); String str = ""; String key = ""; Object value = ""; char[] charList = 域名arArray(); boolean valueBegin = false; for (int i = 0; i < 域名th; i++) { char c = charList[i]; if (c == \'{\') { if (valueBegin == true) { value = getValue(域名tring(i, 域名th())); i = 域名xOf(\'}\', i) + 1; 域名(key, value); } } else if (c == \'=\') { valueBegin = true; key = str; str = ""; } else if (c == \',\') { valueBegin = false; value = str; str = ""; 域名(key, value); } else if (c == \'}\') { if (str != "") { value = str; } 域名(key, value); return map; } else if (c != \' \') { str += c; } } return map; }