1. 程式人生 > >fastjson轉list bean

fastjson轉list bean

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.4</version>

</dependency>

  1. publicclass FastJSON {  
  2.     /** 
  3.      * @param args 
  4.      */
  5.     publicstaticvoid main(String[] args) throws Exception{  
  6.         //string2Json();
  7.         //string2Object();
  8.         //string2List();
  9.         map2json();  
  10.         map2JSON();  
  11.     }  
  12.     /** 
  13.      * 通過fastjson把字串轉換成JSON資料 
  14.      * TypeReference 
  15.      */
  16.     publicstaticvoid string2Json(){  
  17.         StringBuffer buffer=new StringBuffer();  
  18.         buffer.append("{");  
  19.             buffer.append("\"age\":"
    ).append("27").append(",");  
  20.             buffer.append("\"userName\":").append("\"龍叔\"").append(",");  
  21.             buffer.append("\"address\":").append("\"廣東省雲浮市\"");  
  22.         buffer.append("}");  
  23.         String jsonText=buffer.toString();  
  24.         JSONObject jobj=JSON.parseObject(jsonText);  
  25.         String address=jobj.get("address"
    ).toString();  
  26.         System.out.println(address);  
  27.     }  
  28.     /** 
  29.      * 通過fastjson把字串轉換成物件 
  30.      * TypeReference 
  31.      */
  32.     publicstaticvoid string2Object(){  
  33.         StringBuffer buffer=new StringBuffer();  
  34.         buffer.append("{");  
  35.             buffer.append("\"age\":").append("27").append(",");  
  36.             buffer.append("\"userName\":").append("\"龍叔\"").append(",");  
  37.             buffer.append("\"address\":").append("\"廣東省雲浮市\"");  
  38.         buffer.append("}");  
  39.         String jsonText=buffer.toString();  
  40.         //方法一 把json字串轉成Student物件
  41.         Student stu1 = JSON.parseObject(jsonText, new TypeReference<Student>(){});  
  42.         //方法二 把json字串轉成Student物件
  43.         Student stu2 = JSON.parseObject(jsonText,Student.class);    
  44.         System.out.println(stu1.getAddress());  
  45.         System.out.println(stu2.getAddress());  
  46.     }  
  47.     /** 
  48.      * 通過fastjson把字串轉換成泛型陣列 
  49.      * TypeReference 
  50.      */
  51.     publicstaticvoid string2List(){  
  52.         StringBuffer buffer=new StringBuffer();  
  53.         buffer.append("[{");  
  54.             buffer.append("\"age\":").append("27").append(",");  
  55.             buffer.append("\"userName\":").append("\"龍叔\"").append(",");  
  56.             buffer.append("\"address\":").append("\"廣東省雲浮市\"");  
  57.         buffer.append("}]");  
  58.         String jsonText=buffer.toString();  
  59.         //轉成成陣列
  60.         Student[] stu2 = JSON.parseObject(jsonText,new TypeReference<Student[]>(){});    
  61.         List<Student> list = Arrays.asList(stu2);  
  62.         for(Student st:list){  
  63.             System.out.println(st.getAddress());  
  64.         }  
  65.         // 轉換成ArrayList
  66.         ArrayList<Student> list2 = JSON.parseObject(jsonText, new TypeReference<ArrayList<Student>>(){});   
  67.         for (int i = 0; i < list2.size(); i++) {  
  68.             Student obj =(Student) list2.get(i);  
  69.             System.out.println(obj.getAddress());  
  70.         }  
  71.     }  
  72.     /** 
  73.      * 通過fastjson把Map換成字串轉 
  74.      */
  75.     publicstaticvoid map2json(){  
  76.         //建立一個Map物件
  77.          Map<String,String> map = new HashMap<String, String>();  
  78.          map.put("username""周伯通");  
  79.          map.put("address""廣東省仙遊谷");  
  80.          map.put("age""198");  
  81.          String json = JSON.toJSONString(map,true); //轉成JSON資料
  82.          Map<String,String> map1 = (Map<String,String>)JSON.parse(json);   
  83.          //遍歷陣列資料
  84.          for (String key : map1.keySet()) {   
  85.             System.out.println(key+":"+map1.get(key));   
  86.         }   
  87.     }  
  88.     /** 
  89.      * 通過fastjson把Map換成字串轉 
  90.      */
  91.     publicstaticvoid map2JSON() {  
  92.         Map map = new HashMap();  
  93.         map.put("username""周伯通");  
  94.         map.put("address""廣東省仙遊谷");  
  95.         map.put("age""198");  
  96.         String json = JSON.toJSONString(map);  
  97.         Map map1 = JSON.parseObject(json);  
  98.         for (Object obj : map.entrySet()) {  
  99.             Map.Entry<String, String> entry = (Map.Entry<String, String>) obj;  
  100.             System.out.println(entry.getKey() + "--->" + entry.getValue());  
  101.         }  
  102.     }   
  103. }  


[java] view plain copy  print?
  1. package ivyy.taobao.com.entity;  
  2. import java.io.Serializable;  
  3. /** 
  4.  *@Author:liangjl 
  5.  *@Date:2014-12-19 
  6.  *@Version:1.0 
  7.  *@Description: 
  8.  */
  9. publicclass Student implements Serializable{  
  10.     private Integer age;  
  11.     private String sex;  
  12.     private String userName;  
  13.     private String birthday;  
  14.     private String address;  
  15.     private String email;  
  16.     public Integer getAge() {  
  17.         return age;  
  18.     }  
  19.     publicvoid setAge(Integer age) {  
  20.         this.age = age;  
  21.     }  
  22.     public String getSex() {  
  23.         return sex;  
  24.     }  
  25.     publicvoid setSex(String sex) {  
  26.         this.sex = sex;  
  27.     }  
  28.     public String getUserName() {  
  29.         return userName;  
  30.     }  
  31.     publicvoid setUserName(Stri