闲话少说,通过实例简单介绍一下org.json的用法,用到的jar包是json-20090211.jar
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; /** * @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(); } 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", "IluckySi"); jo.put("age", 27); jo.put("sex", 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 + ", 年龄 = " + age + ", 性别 = " + (sex == true ? "男" : "女")); } } public static void testComplex() throws JSONException { JSONObject jo = new JSONObject(); jo.put("username", "IluckySi"); jo.put("age", 27); jo.put("sex", true); Map<Object, Object> message = new HashMap<Object, Object>(); message.put("result", "success"); message.put("message", "测试"); 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", "name1"); user2.put("password", "password1"); list.add(user2); message.put("users", list); jo.put("message", message); String send = jo.toString(); System.out.println("要测试的消息" + send); send(send); } private static void send(String send) throws JSONException { JSONObject jo = new JSONObject(send); String username = jo.getString("username"); int age = jo.getInt("age"); boolean sex = jo.getBoolean("sex"); JSONObject u2 = new JSONObject(jo.getString("message")); String result = u2.getString("result"); String message = u2.getString("message"); System.out.println(username + ", 年龄 = " + age + ", 性别 = " + (sex == true ? "男" : "女") + ", result=" + result + ", message =" + message); String users = u2.getString("users"); JSONArray ja = new JSONArray(users); 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); } } } /** 输出结果: -----test1----- IluckySi, 年龄 = 27, 性别 = 男, 技能如下: javascript: 凑合 java: 不错 jquery: 挺好 -----test2----- IluckySi, 年龄 = 27, 性别 = 男 IluckySi2, 年龄 = 28, 性别 = 女 -----test3----- 要测试的消息{"message":{"message":"测试","result":"success","users":[{"name":"name1","password":"password1"},{"name":"name1","password":"password1"}]},"sex":true,"username":"IluckySi","age":27} IluckySi, 年龄 = 27, 性别 = 男, result=success, message =测试 name = name1, password = password1 name = name1, password = password1 */
时间: 2024-11-12 18:06:32