Java對JSON的簡單操作
JSONArray和JSONObject
基本用法
package com.cloud.test; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Demo1 { //建立JSONObject物件 public static JSONObject createJSONObject(){ JSONObject jsonObject = new JSONObject(); jsonObject.put("username", "Spring"); jsonObject.put("sex" jsonObject.put("QQ", "123456"); return jsonObject; } @SuppressWarnings("static-access") public static void main(String[] args) throws Exception { //建立一個JSONObject物件 JSONObject jsonObject = Demo1.createJSONObject(); //輸出jsonObject:{"sex":"男","username":"Spring","QQ":"123456"} System.out.println(jsonObject); //判斷jsonObject的物件類 System.out.println("陣列?"+jsonObject.isArray()+";為空?"+ jsonObject.isEmpty()+";isNullObject?"+ jsonObject.isNullObject()+";"); //追加元素屬性 jsonObject.put("address", "安徽合肥"); System.out // 返回一個JSONArray物件 JSONArray jsonArray = new JSONArray(); jsonArray.add(0, "Value0"); jsonArray.add(1, "Value1"); jsonObject.element("jsonArray", jsonArray); //在jsonObject後面住家一個jsonArray JSONArray array = jsonObject.getJSONArray("jsonArray"); System.out.println(jsonObject); System.out.println("返回一個JSONArray物件:" + array); //根據key返回字串 String username = jsonObject.getString("username"); System.out.println("username:"+username); //字串轉換為jsonObject String temp = jsonObject.toString(); System.out.println(temp); JSONObject object = jsonObject.fromObject(temp); System.out.println("QQ:"+object.get("QQ")); } } |
案例
package com.cloud.test; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Demo2 { public static void main(String[] args) { JSONObject jsonObject0 = new JSONObject(); JSONObject jsonObject1 = new JSONObject(); JSONObject jsonObject2 = new JSONObject(); JSONObject jsonObject3 = new JSONObject(); JSONArray jsonArray = new JSONArray(); //jsonObject0 jsonObject0.put("name0", "Spring"); jsonObject0.put("sex0", "man"); System.out.println("jsonObject0:"+jsonObject0); //jsonObject1 jsonObject1.put("name1", "summer"); jsonObject1.put("sex1", "woman"); System.out.println("jsonObject1"+jsonObject1); //jsonObject2 jsonObject2.put("item0", jsonObject0); jsonObject2.put("item1", jsonObject1); System.out.println("jsonObject2"+jsonObject2); //jsonObject3 jsonObject3.put("jo31", jsonObject2); jsonObject3.put("jo32", jsonObject3); System.out.println("jsonObject3"+jsonObject3); //往JSONArray中新增JSONObject物件。發現JSONArray跟JSONObject的區別就是JSONArray比JSONObject多中括號[] jsonArray.add(jsonObject1); System.out.println(jsonArray); JSONObject jsonObject4 = new JSONObject(); jsonObject4.element("weater", jsonArray); System.out.println("jsonObject4:"+jsonObject4); } } |
FromObject和toBean
fromObject
package com.cloud.test; import java.util.HashMap; import java.util.Map; import net.sf.json.JSONObject; public class fo { @SuppressWarnings("rawtypes") public static void main(String[] args) { Map map = new HashMap(); map.put("spring", "spring1"); map.put("summer", "summer1"); map.put("autumn", "autumn1"); JSONObject json = JSONObject.fromObject(map); //{"autumn":"autumn1","summer":"summer1","spring":"spring1"} System.out.println(json); } } |
toBean
student.java實體類
package com.cloud.test; public class Student{ private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString(){ return this.id+";"+this.name+";"+this.age; } } |
tb.java
package com.cloud.test; import net.sf.json.JSONObject; public class tb { public static void main(String[] args) { String json = "{id:'101',name:'你好',age:'22'}"; Student stu = new Student(); JSONObject jsonObject = JSONObject.fromObject(json); stu = (Student) JSONObject.toBean(jsonObject, Student.class); //101;你好;22 System.out.println(stu); String json1 = "{id:'101',name:'張三'}"; Student stu1 = new Student(); JSONObject obj = JSONObject.fromObject(json1); System.out.println("obj:"+obj); stu1 = (Student)JSONObject.toBean(obj, Student.class); //101;張三;0 System.out.println(stu1); String json2 = "{id:'101',age:'22'}"; Student stu2 = new Student(); JSONObject obj1 = JSONObject.fromObject(json2); stu2 = (Student)JSONObject.toBean(obj1, Student.class); //101;null;22 System.out.println(stu2); String json3 = "{id:'101',name:'張三',age:'nn'}"; Student stu3 = new Student(); JSONObject obj2 = JSONObject.fromObject(json3); stu3 = (Student)JSONObject.toBean(obj2, Student.class); //101;張三;0 System.out.println(stu3); String json4 = "{id:'101',name:'張三',age:'22',sex:'男'}"; Student stu4 = new Student(); JSONObject obj3 = JSONObject.fromObject(json4); stu4 = (Student)JSONObject.toBean(obj3, Student.class); //101;張三;22 System.out.println(stu4); } } |