String转换成json格式(二):org.codehaus.jettison.json.JSONObject

来源:互联网 发布:制作印章的软件 编辑:程序博客网 时间:2024/06/05 09:53
实例:
//将str转换成JSONObjct格式public static void main(String [] args){String str = "{\"result\":\"success\",\"message\":\"成功!\"}";JSONObject json;try {json = new JSONObject(str);System.out.println(json);} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

结果:


实例:
//将str转换成JSONArraypublic static void main(String [] args){String str = "{\"result\":\"success\",\"message\":\"成功!\",\"data\":[{\"name\":\"Tom\",\"age\":\"20\"}]}";JSONObject json;try {json = new JSONObject(str);System.out.println(json);JSONArray jsonArray = new JSONArray(json.getString("data"));System.out.println(jsonArray.toString());} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}


结果:




0 0