闲话少说,通过实例简单介绍一下org.json的用法,用到的jar包是json-20090211.jar
<pre name="code" class="java">package com.ilucky.util.orgjson; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /** * 重点:JSONObject对应集合Map,JSONArray对应集合List. * 用json类(fastjson和orgjson)库处理复杂的集合时,要将集合直接放到value处,不能将其转换为json字符串放到value处,* 如果将其转换为字符串放到value处,最终生成的json字符串会带有转义字符. * @author IluckySi * @since 20150407 */ public class OrgjsonUtil { public static void main(String[] args) { try { //JSONObject的用法. System.out.println("-----test1-----"); testJsonObject(); //JSONArray的用法. System.out.println("-----test2-----"); testJsonArray(); //JSONObject,JSONArray和集合的复杂用法. System.out.println("-----test3-----"); testComplex(); //JSONObject,JSONArray和集合的复杂用法. System.out.println("-----test4-----"); testComplex2(); } catch (Exception e) { System.out.println("测试发生异常: " + e); } } public static void testJsonObject() throws JSONException { JSONObject jo = new JSONObject(); jo.put("username","IluckySi"); jo.put("age",27); jo.put("sex",true); Map<String,String> skill = new HashMap<String,String>(); skill.put("java","不错"); skill.put("javascript","凑合"); skill.put("jquery","挺好"); jo.put("skill",skill); String username = jo.getString("username"); int age = jo.getInt("age"); boolean sex = jo.getBoolean("sex"); JSONObject skill2 = new JSONObject(jo.getString("skill")); System.out.println(username + ",年龄 = " + age + ",性别 = " + (sex == true ? "男" : "女") + ",技能如下:"); String[] names = JSONObject.getNames(skill2); for(String name : names) { System.out.println(name + ": " + skill2.getString(name)); } } public static void testJsonArray() throws JSONException { JSONArray ja = new JSONArray(); JSONObject jo = new JSONObject(); jo.put("username",true); ja.put(jo); JSONObject jo2 = new JSONObject(); jo2.put("username","IluckySi2"); jo2.put("age",28); jo2.put("sex",false); ja.put(jo2); for(int i = 0; i < ja.length(); i++) { JSONObject j = (JSONObject)ja.get(i); String username = j.getString("username"); int age = j.getInt("age"); boolean sex = j.getBoolean("sex"); System.out.println(username + ",性别 = " + (sex == true ? "男" : "女")); } } public static void testComplex() throws JSONException { JSONObject jo = new JSONObject(); jo.put("result","success"); List<Map<Object,Object>> list = new ArrayList<Map<Object,Object>>(); Map<Object,Object> user1 = new HashMap<Object,Object>(); user1.put("name","name1"); user1.put("password","password1"); list.add(user1); Map<Object,Object> user2 = new HashMap<Object,Object>(); user2.put("name","name2"); user2.put("password","password2"); list.add(user2); jo.put("message",list); String send = jo.toString(); System.out.println("要测试的消息" + send); send(send); } private static void send(String send) throws JSONException { JSONObject jo = new JSONObject(send); String result = jo.getString("result"); System.out.println("result=" + result); JSONArray ja = new JSONArray(jo.getString("message")); for(int i = 0; i < ja.length(); i++) { JSONObject j = (JSONObject)ja.get(i); String name = j.getString("name"); String password = j.getString("password"); System.out.println("name = " + name + ",password = " + password); } } public static void testComplex2() throws JSONException { JSONObject jo = new JSONObject(); jo.put("result","success"); JSONObject message = new JSONObject(); List<Map<Object,"password2"); list.add(user2); message.put("message",list); jo.put("message",message.toString());//注意:如果直接写message,也不会有转义字符. String send = jo.toString(); System.out.println("要测试的消息" + send); send2(send); } private static void send2(String send) throws JSONException { JSONObject jo = new JSONObject(send); String result = jo.getString("result"); System.out.println("result=" + result); JSONObject message = new JSONObject(jo.getString("message")); JSONArray message2 = new JSONArray(message.getString("message")); for(int i = 0; i < message2.length(); i++) { JSONObject j = (JSONObject)message2.get(i); String name = j.getString("name"); String password = j.getString("password"); System.out.println("name = " + name + ",password = " + password); } } } /** 输出结果: -----test1----- IluckySi,年龄 = 27,性别 = 男,技能如下: javascript: 凑合 java: 不错 jquery: 挺好 -----test2----- IluckySi,性别 = 男 IluckySi2,年龄 = 28,性别 = 女 -----test3----- 要测试的消息{"message":[{"name":"name1","password":"password1"},{"name":"name2","password":"password2"}],"result":"success"} result=success name = name1,password = password1 name = name2,password = password2 -----test4----- 要测试的消息{"message":"{\"message\":[{\"name\":\"name1\",\"password\":\"password1\"},{\"name\":\"name2\",\"password\":\"password2\"}]}",password = password2 */