[java] view plaincopy
 
  1. import java.util.ArrayList;  
  2. import java.util.Collection;  
  3. import java.util.Iterator;  
  4. import java.util.List;  
  5.   
  6. import net.sf.json.JSONArray;  
  7. import net.sf.json.JSONObject;  
  8.   
  9. public class MainClass {  
  10.   
  11.     public static void main(String[] args) {  
  12.         TestJsonBean();  
  13.         TestJsonAttribute();  
  14.         TestJsonArray();          
  15.     }  
  16.   
  17.     @SuppressWarnings("rawtypes")  
  18.     private static void TestJsonArray() {  
  19.         Student student1 = new Student();  
  20.         student1.setId(1);  
  21.         student1.setName("jag");  
  22.         student1.setSex("man");  
  23.         student1.setAge(25);  
  24.         student1.setHobby(new String[]{"篮球","游戏"});  
  25.           
  26.         Student student2 = new Student();  
  27.         student2.setId(2);  
  28.         student2.setName("tom");  
  29.         student2.setSex("woman");  
  30.         student2.setAge(23);  
  31.         student2.setHobby(new String[]{"上网","跑步"});  
  32.           
  33.         List<Student> list = new ArrayList<Student>();  
  34.         list.add(student1);  
  35.         list.add(student2);  
  36.           
  37.         JSONArray jsonArray = JSONArray.fromObject(list);  
  38.         System.out.println(jsonArray.toString());  
  39.           
  40.         JSONArray new_jsonArray=JSONArray.fromObject(jsonArray.toArray());  
  41.         Collection java_collection=JSONArray.toCollection(new_jsonArray);  
  42.         if(java_collection!=null && !java_collection.isEmpty())  
  43.         {  
  44.             Iterator it=java_collection.iterator();  
  45.             while(it.hasNext())  
  46.             {  
  47.                 JSONObject jsonObj=JSONObject.fromObject(it.next());  
  48.                 Student stu=(Student) JSONObject.toBean(jsonObj,Student.class);  
  49.                 System.out.println(stu.getName());  
  50.             }  
  51.         }  
  52.     }  
  53.   
  54.     private static void TestJsonAttribute() {  
  55.         /** 
  56.          * 创建json对象并为该对象设置属性 
  57.          */  
  58.         JSONObject jsonObj = new JSONObject();  
  59.         jsonObj.put("Int_att",25);//添加int型属性  
  60.         jsonObj.put("String_att","str");//添加string型属性  
  61.         jsonObj.put("Double_att",12.25);//添加double型属性  
  62.         jsonObj.put("Boolean_att",true);//添加boolean型属性  
  63.         //添加JSONObject型属性  
  64.         JSONObject jsonObjSon = new JSONObject();  
  65.         jsonObjSon.put("id", 1);  
  66.         jsonObjSon.put("name", "tom");  
  67.         jsonObj.put("JSONObject_att",jsonObjSon);  
  68.         //添加JSONArray型属性  
  69.         JSONArray jsonArray = new JSONArray();  
  70.         jsonArray.add("array0");  
  71.         jsonArray.add("array1");  
  72.         jsonArray.add("array2");  
  73.         jsonArray.add("array3");  
  74.         jsonObj.put("JSONArray_att", jsonArray);  
  75.         System.out.println(jsonObj.toString());  
  76.         System.out.println("Int_att:"+jsonObj.getInt("Int_att"));  
  77.         System.out.println("String_att:"+jsonObj.getString("String_att"));  
  78.         System.out.println("Double_att:"+jsonObj.getDouble("Double_att"));  
  79.         System.out.println("Boolean_att:"+jsonObj.getBoolean("Boolean_att"));  
  80.         System.out.println("JSONObject_att:"+jsonObj.getJSONObject("JSONObject_att"));  
  81.         System.out.println("JSONArray_att:"+jsonObj.getJSONArray("JSONArray_att"));  
  82.     }  
  83.   
  84.     /** 
  85.      * java对象与json对象互相转换 
  86.      */  
  87.     private static void TestJsonBean() {  
  88.         /** 
  89.          * 创建java对象 
  90.          */  
  91.         Student student = new Student();  
  92.         student.setId(1);  
  93.         student.setName("jag");  
  94.         student.setSex("man");  
  95.         student.setAge(25);  
  96.         student.setHobby(new String[]{"篮球","上网","跑步","游戏"});  
  97.         /** 
  98.          * java对象转换成json对象,并获取json对象属性 
  99.          */  
  100.         JSONObject jsonStu = JSONObject.fromObject(student);  
  101.         System.out.println(jsonStu.toString());  
  102.         System.out.println(jsonStu.getJSONArray("hobby"));  
  103.         /** 
  104.          * json对象转换成java对象,并获取java对象属性 
  105.          */  
  106.         Student stu = (Student) JSONObject.toBean(jsonStu, Student.class);  
  107.         System.out.println(stu.getName());  
  108.         /** 
  109.          * 创建json对象 
  110.          */  
  111.         JSONObject jsonObj = new JSONObject();  
  112.         jsonObj.put("id",1);  
  113.         jsonObj.put("name","张勇");  
  114.         jsonObj.put("sex","男");  
  115.         jsonObj.put("age",24);  
  116.         //jsonObj.put("hobby",new String[]{"上网","游戏","跑步","音乐"});  
  117.         //System.out.println(jsonObj.toString());  
  118.         /** 
  119.          * json对象转换成java对象 
  120.          */  
  121.         Student stud = (Student) JSONObject.toBean(jsonObj,Student.class);  
  122.         System.out.println(stud.getName());       
  123.     }  
  124. }