实例:
//将str转换成JSONObjct格式
public static void main(String [] args){
String str = "{\"result\":\"success\",\"message\":\"成功!\"}";
JSONObject json = JSONObject.fromObject(str);
System.out.println(json.toString());
}
结果:
实例:
//将str转换成JSONArray
public static void main(String [] args){
String str = "{\"result\":\"success\",\"message\":\"成功!\",\"data\":[{\"name\":\"Tom\",\"age\":\"20\"}]}";
JSONObject json = JSONObject.fromObject(str);
System.out.println(json);
JSONArray jsonArray = JSONArray.fromObject(json.getString("data"));
System.out.println(jsonArray.toString());
}
结果: