public static void main(String[] args) { String str = "{src_text=小牛上传,tgt_text=Ewota eswi,from=zh,to=izz,username=1109}"; Map<String, String> map = mapStringToMap(str); System.out.println(map); System.out.println(map.get("file_url")); System.out.println(map.get("src_text")); System.out.println(map.get("tgt_text")); System.out.println(map.get("from")); System.out.println(map.get("to")); System.out.println(map.get("username")); } public static Map<String, String> mapStringToMap(String str) { str = str.substring(1, str.length() - 1); String[] strs = str.split(","); Map<String, String> map = new HashMap<String, String>(); for (String string : strs) { String key = string.split("=")[0]; String value = string.split("=")[1]; map.put(key, value); } return map; }
【结果展示】