JSON使用方法举例
例子1:
1 import net.sf.json.JSONArray; 2 import net.sf.json.JSONObject; 3 4 /** 5 * Created by leiqiang on 2014/7/25. 6 */ 7 public class JSONObjectSample { 8 // 创建JSONObject对象 9 private static JSONObject createJSONObject() { 10 JSONObject jsonObject = new JSONObject(); 11 jsonObject.put("username", "leiqiang"); 12 jsonObject.put("sex", "男"); 13 jsonObject.put("nickname", "lqPROplus"); 14 return jsonObject; 15 } 16 17 public static void main(String[] args) { 18 JSONObject jsonObject = JSONObjectSample.createJSONObject();//静待方法,直接通过类名+方法调用 19 // 输出jsonobject对象 20 System.out.println("jsonObject:" + jsonObject); 21 22 // 判读输出对象的类型 23 boolean isArray = jsonObject.isArray(); 24 boolean isEmpty = jsonObject.isEmpty(); 25 boolean isNullObject = jsonObject.isNullObject(); 26 System.out.println("是否为数组:" + isArray + ", 是否为空:" + isEmpty 27 + ", isNullObject:" + isNullObject); 28 29 // 添加属性,在jsonObject后面追加元素。 30 jsonObject.element("address", "北京市"); 31 System.out.println("添加属性后的对象:" + jsonObject); 32 33 // 返回一个JSONArray对象 34 JSONArray jsonArray = new JSONArray(); 35 jsonArray.add(0, "this is a jsonArray value"); 36 jsonArray.add(1, "another jsonArray value"); 37 jsonObject.element("jsonArray", jsonArray); 38 //在jsonObject后面加一个jsonArray 39 JSONArray array = jsonObject.getJSONArray("jsonArray"); 40 //System.out.println("加入jsonArray后的对象:"+jsonObject); 41 42 43 System.out.println("返回一个JSONArray对象:" + array); 44 // 添加JSONArray后的值 45 System.out.println("结果=" + jsonObject); 46 47 // 根据key返回一个字符串 48 String username = jsonObject.getString("username"); 49 System.out.println("username=" + username); 50 51 // 把字符转换为 JSONObject 52 String temp = jsonObject.toString(); 53 JSONObject object = JSONObject.fromObject(temp); 54 55 // 转换后根据Key返回值 56 System.out.println("nickname=" + object.get("nickname")); 57 58 } 59 }
运行结果:
1 jsonObject:{"username":"leiqiang","sex":"男","nickname":"lqPROplus"} 2 是否为数组:false, 是否为空:false, isNullObject:false 3 添加属性后的对象:{"username":"leiqiang","sex":"男","nickname":"lqPROplus","address":"北京市"} 4 返回一个JSONArray对象:["this is a jsonArray value","another jsonArray value"] 5 结果={"username":"leiqiang","sex":"男","nickname":"lqPROplus","address":"北京市","jsonArray":["this is a jsonArray value","another jsonArray value"]} 6 username==>leiqiang 7 nickname=lqPROplus
例子2:
1 import net.sf.json.JSONArray; 2 import net.sf.json.JSONObject; 3 4 /** 5 * Created by leiqiang on 2014/7/25. 6 */ 7 public class JSONTest { 8 public static void main(String args[]) { 9 JSONObject jsonObj0 = new JSONObject(); 10 JSONObject jsonObj1 = new JSONObject(); 11 JSONObject jsonObj2 = new JSONObject(); 12 JSONObject jsonObj3 = new JSONObject(); 13 JSONArray jsonArray = new JSONArray(); 14 15 //创建jsonObj0 16 jsonObj0.put("name0", "zhangsan"); 17 jsonObj0.put("sex1", "female"); 18 System.out.println("jsonObj0:" + jsonObj0); 19 20 //创建jsonObj1 21 jsonObj1.put("name", "lisi"); 22 jsonObj1.put("sex", "male"); 23 System.out.println("jsonObj:" + jsonObj1); 24 25 //创建jsonObj2,包含两个条目,条目内容分别为jsonObj0,jsonObj1 26 jsonObj2.put("item0", jsonObj0); 27 jsonObj2.put("item1", jsonObj1); 28 System.out.println("jsonObj2:" + jsonObj2); 29 30 //创建jsonObj3,只有一个条目,内容为jsonObj2 31 jsonObj3.element("j3", jsonObj2); 32 System.out.println("jsonObj3:" + jsonObj3); 33 34 //往JSONArray中添加JSONObject对象。发现JSONArray跟JSONObject的区别就是JSONArray比JSONObject多中括号[] 35 jsonArray.add(jsonObj1); 36 System.out.println("jsonArray:" + jsonArray); 37 38 JSONObject jsonObj4 = new JSONObject(); 39 jsonObj4.element("weather", jsonArray); 40 System.out.println("jsonObj4:" + jsonObj4); 41 } 42 }
运行结果:
1 jsonObj0:{"name0":"zhangsan","sex1":"female"} 2 jsonObj:{"name":"lisi","sex":"male"} 3 jsonObj2:{"item0":{"name0":"zhangsan","sex1":"female"},"item1":{"name":"lisi","sex":"male"}} 4 jsonObj3:{"j3":{"item0":{"name0":"zhangsan","sex1":"female"},"item1":{"name":"lisi","sex":"male"}}} 5 jsonArray:[{"name":"lisi","sex":"male"}] 6 jsonObj4:{"weather":[{"name":"lisi","sex":"male"}]}
例子3:
1 import net.sf.json.JSONObject; 2 3 /** 4 * Created by leiqiang on 2014/7/25. 5 */ 6 public class JSONfromObject { 7 public static void main(String[] args) { 8 9 JSONObject object = JSONObject.fromObject("{\"username\":\"leiqiang\",\"sex\":\"男\",\"nickname\":\"lqPROplus\"}"); 10 System.out.println("nickname=" + object.get("nickname")); 11 12 System.out.println(object.getString("username")); 13 14 JSONObject object1 = new JSONObject(); 15 object1.put("a",object); 16 System.out.println(object1); 17 } 18 19 }
运行结果:
1 nickname=lqPROplus 2 leiqiang 3 {"a":{"username":"leiqiang","sex":"男","nickname":"lqPROplus"}}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。