乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      java對象和json對象之間互相轉(zhuǎn)換

       擎天豬mpnlajkd 2016-09-11
      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[]{"上網(wǎng)","跑步"});  
      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.          * 創(chuàng)建json對象并為該對象設(shè)置屬性 
      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對象互相轉(zhuǎn)換 
      86.      */  
      87.     private static void TestJsonBean() {  
      88.         /** 
      89.          * 創(chuàng)建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[]{"籃球","上網(wǎng)","跑步","游戲"});  
      97.         /** 
      98.          * java對象轉(zhuǎn)換成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對象轉(zhuǎn)換成java對象,并獲取java對象屬性 
      105.          */  
      106.         Student stu = (Student) JSONObject.toBean(jsonStu, Student.class);  
      107.         System.out.println(stu.getName());  
      108.         /** 
      109.          * 創(chuàng)建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[]{"上網(wǎng)","游戲","跑步","音樂"});  
      117.         //System.out.println(jsonObj.toString());  
      118.         /** 
      119.          * json對象轉(zhuǎn)換成java對象 
      120.          */  
      121.         Student stud = (Student) JSONObject.toBean(jsonObj,Student.class);  
      122.         System.out.println(stud.getName());       
      123.     }  
      124. }  


        

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多