net.sf.json.JSONObject使用

转载自:https://blog.csdn.net/u013514928/article/details/79114814/

JSONObjetc和JSONArray存在多种用法,下面介绍JSONObject和JSONArray的创建、合并以及解析。

[java]  view plain  copy
  1. import java.util.ArrayList;  
  2. import java.util.HashMap;  
  3.   
  4. import net.sf.json.JSONArray;  
  5. import net.sf.json.JSONObject;  
  6.   
  7. public class JsonCombine {  
  8.   
  9.     public static void main(String[] args) {  
  10.         JSONObject jsonOne = new JSONObject();  
  11.         jsonOne.put("name""kewen");  
  12.         jsonOne.put("age""24");  
  13.         JSONObject jsonTwo = new JSONObject();  
  14.         jsonTwo.put("hobbit""Doto");  
  15.         jsonTwo.put("hobbit2""wow");  
  16.         JSONObject jsonThree = new JSONObject();  
  17.         jsonThree.putAll(jsonOne);  
  18.         jsonThree.putAll(jsonTwo);  
  19.         System.out.println(jsonThree.toString());  
  20.   
  21.         // JsonObject和JsonArray区别就是JsonObject是对象形式,JsonArray是数组形式  
  22.         // 创建JsonObject第一种方法  
  23.         JSONObject jsonObject = new JSONObject();  
  24.         jsonObject.put("UserName""ZHULI");  
  25.         jsonObject.put("age""30");  
  26.         jsonObject.put("workIn""ALI");  
  27.         System.out.println("jsonObject1:" + jsonObject);  
  28.   
  29.         // 创建JsonObject第二种方法  
  30.         HashMap hashMap = new HashMap();  
  31.         hashMap.put("UserName""ZHULI");  
  32.         hashMap.put("age""30");  
  33.         hashMap.put("workIn""ALI");  
  34.         System.out.println("jsonObject2:" + JSONObject.fromObject(hashMap));  
  35.   
  36.         // 创建一个JsonArray方法1  
  37.         JSONArray jsonArray = new JSONArray();  
  38.         jsonArray.add(0"ZHULI");  
  39.         jsonArray.add(1"30");  
  40.         jsonArray.add(2"ALI");  
  41.         System.out.println("jsonArray1:" + jsonArray);  
  42.   
  43.         // 创建JsonArray方法2  
  44.         ArrayList arrayList = new ArrayList();  
  45.         arrayList.add("ZHULI");  
  46.         arrayList.add("30");  
  47.         arrayList.add("ALI");  
  48.         System.out.println("jsonArray2:" + JSONArray.fromObject(arrayList));  
  49.         // 如果JSONArray解析一个HashMap  
  50.         System.out.println("jsonArray FROM HASHMAP:" + JSONArray.fromObject(hashMap));  
  51.         // 组装一个复杂的JSONArray  
  52.         JSONObject jsonObject2 = new JSONObject();  
  53.         jsonObject2.put("UserName""ZHULI");  
  54.         jsonObject2.put("age""30");  
  55.         jsonObject2.put("workIn""ALI");  
  56.         jsonObject2.element("Array", arrayList);  
  57.         jsonObject2.element("Map", hashMap);  
  58.         System.out.println("jsonObject2:" + jsonObject2);  
  59.   
  60.         // 将Json字符串转为java对象  
  61.         String jsonString = "{\"UserName\":\"ZHULI\",\"age\":\"30\",\"workIn\":\"ALI\",\"Array\":[\"ZHULI\",\"30\",\"ALI\"]}";  
  62.         JSONObject obj = JSONObject.fromObject(jsonString);  
  63.         if (obj.has("UserName")) {  
  64.             System.out.println("UserName:" + obj.getString("UserName"));  
  65.         }  
  66.         if (obj.has("Array")) {  
  67.             JSONArray transitListArray = obj.getJSONArray("Array");  
  68.             for (int i = 0; i < transitListArray.size(); i++) {  
  69.                 System.out.println("Array:" + transitListArray.getString(i) + " ");  
  70.   
  71.             }  
  72.         }  
  73.   
  74.     }  
  75.   
  76. }  

       运行结果:

[java]  view plain  copy
  1. {"name":"kewen","age":"24","hobbit":"Doto","hobbit2":"wow"}  
  2. jsonObject1:{"UserName":"ZHULI","age":"30","workIn":"ALI"}  
  3. jsonObject2:{"UserName":"ZHULI","workIn":"ALI","age":"30"}  
  4. jsonArray1:["ZHULI","30","ALI"]  
  5. jsonArray2:["ZHULI","30","ALI"]  
  6. jsonArray FROM HASHMAP:[{"UserName":"ZHULI","workIn":"ALI","age":"30"}]  
  7. jsonObject2:{"UserName":"ZHULI","age":"30","workIn":"ALI","Array":["ZHULI","30","ALI"],"Map":{"UserName":"ZHULI","workIn":"ALI","age":"30"}}  
  8. UserName:ZHULI  
  9. Array:ZHULI   
  10. Array:30   
  11. Array:ALI   

optXX(key,defaultValue)区别于getXX(key) ,getXX(key)如果不存在key会抛出异常,

optXX()会返回defafltValue

上代码:

String jsonStr="{\"name\":\"xy\",\"age\":24}";
		JSONObject  json= JSONObject.fromObject(jsonStr);
		System.out.println("json.opt(name)---"+json.opt("name"));
		System.out.println("json.optInt(age, 22)---"+json.optInt("age", 22));
		System.out.println("json.optString(age)---"+json.optString("age"));
		System.out.println("json.opt(aaa)---"+json.opt("aaa"));

执行结果:

json.opt(name)---xy
json.optInt(age, 22)---24
json.optString(age)---24
json.opt(aaa)---null




你可能感兴趣的:(java)