1、String转JSON
String str ="{\"abc\":\"1\",\"hahah\":\"2\"}";
JSONObject strJson= JSONObject.fromObject(str);
JSONObject.fromObject() 所需jar包连接
JSON-LIB
2、JSON转Map
public static void main(String[] args) throws Exception {
String str = {"firstName":"Bill","lastName":"Gates"};
cutSemiString(str);
}
//根据自己的所需做改动
public static Map<String, String> cutSemiString(String str) {
String array[] = str.substring(str.indexOf("{") + 1, str.indexOf("}")).split(", *");
Map<String, String> map = new HashMap<String, String>();
for (String i : array) {
String key = i.split(":")[0].replace("\"", "");
String value = i.split(":")[1].replace("\"", "");
map.put(key, value);
}
return map;
}