Json轉換利器Gson之實例五-實際開發中的特殊需求處理(手動解析-TypeToken)

前面四篇博客基本上可以滿足我們處理的絕大多數需求,但有時項目中對json有特殊的格式規定.比如下面的json串解析:

[{"tableName":"students","tableData":[{"id":1,"name":"李坤","birthDay":"Jun 22, 2012 9:54:49 PM"},{"id":2,"name":"曹貴生","birthDay":"Jun 22, 2012 9:54:49 PM"},{"id":3,"name":"柳波","birthDay":"Jun 22, 2012 9:54:49 PM"}]},{"tableName":"teachers","tableData":[{"id":1,"name":"米老師","title":"教授"},{"id":2,"name":"丁老師","title":"講師"}]}]

分析之後我們發現使用前面博客中用到的都不好處理上面的json串.請看本文是如何處理的吧:

實體類:

[java] view plain copy
  1. import java.util.Date;  
  2.   
  3. public class Student {  
  4.     private int id;  
  5.     private String name;  
  6.     private Date birthDay;  
  7.   
  8.     public int getId() {  
  9.         return id;  
  10.     }  
  11.   
  12.     public void setId(int id) {  
  13.         this.id = id;  
  14.     }  
  15.   
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.   
  20.     public void setName(String name) {  
  21.         this.name = name;  
  22.     }  
  23.   
  24.     public Date getBirthDay() {  
  25.         return birthDay;  
  26.     }  
  27.   
  28.     public void setBirthDay(Date birthDay) {  
  29.         this.birthDay = birthDay;  
  30.     }  
  31.   
  32.     @Override  
  33.     public String toString() {  
  34.         return "Student [birthDay=" + birthDay + ", id=" + id + ", name="  
  35.                 + name + "]";  
  36.     }  
  37.   
  38. }  

[java] view plain copy
  1. public class Teacher {  
  2.     private int id;  
  3.   
  4.     private String name;  
  5.   
  6.     private String title;  
  7.   
  8.     public int getId() {  
  9.         return id;  
  10.     }  
  11.   
  12.     public void setId(int id) {  
  13.         this.id = id;  
  14.     }  
  15.   
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.   
  20.     public void setName(String name) {  
  21.         this.name = name;  
  22.     }  
  23.   
  24.     public String getTitle() {  
  25.         return title;  
  26.     }  
  27.   
  28.     public void setTitle(String title) {  
  29.         this.title = title;  
  30.     }  
  31.   
  32.     @Override  
  33.     public String toString() {  
  34.         return "Teacher [id=" + id + ", name=" + name + ", title=" + title  
  35.                 + "]";  
  36.     }  
  37.   
  38. }  
注意這裏定義了一個TableData實體類:

[java] view plain copy
  1. import java.util.List;  
  2.   
  3. public class TableData {  
  4.   
  5.     private String tableName;  
  6.   
  7.     private List tableData;  
  8.   
  9.     public String getTableName() {  
  10.         return tableName;  
  11.     }  
  12.   
  13.     public void setTableName(String tableName) {  
  14.         this.tableName = tableName;  
  15.     }  
  16.   
  17.     public List getTableData() {  
  18.         return tableData;  
  19.     }  
  20.   
  21.     public void setTableData(List tableData) {  
  22.         this.tableData = tableData;  
  23.     }  
  24. }  

測試類:

(仔細看將json轉回爲對象的實現,這裏經過兩次轉化,第一次轉回的結果是map不是我們所期望的對象,對map再次轉爲json後再轉爲對象,我引用的是Gson2.1的jar處理正常,好像使用Gson1.6的jar會報錯,所以建議用最新版本)

[java] view plain copy
  1. import java.util.ArrayList;  
  2. import java.util.Date;  
  3. import java.util.List;  
  4.   
  5. import com.google.gson.Gson;  
  6. import com.google.gson.reflect.TypeToken;  
  7.   
  8. public class GsonTest5 {  
  9.   
  10.     /** 
  11.      * @param args 
  12.      */  
  13.     public static void main(String[] args) {  
  14.         // 對象轉爲Json-->start  
  15.         Student student1 = new Student();  
  16.         student1.setId(1);  
  17.         student1.setName("李坤");  
  18.         student1.setBirthDay(new Date());  
  19.   
  20.         Student student2 = new Student();  
  21.         student2.setId(2);  
  22.         student2.setName("曹貴生");  
  23.         student2.setBirthDay(new Date());  
  24.   
  25.         Student student3 = new Student();  
  26.         student3.setId(3);  
  27.         student3.setName("柳波");  
  28.         student3.setBirthDay(new Date());  
  29.   
  30.         List<Student> stulist = new ArrayList<Student>();  
  31.         stulist.add(student1);  
  32.         stulist.add(student2);  
  33.         stulist.add(student3);  
  34.   
  35.         Teacher teacher1 = new Teacher();  
  36.         teacher1.setId(1);  
  37.         teacher1.setName("米老師");  
  38.         teacher1.setTitle("教授");  
  39.   
  40.         Teacher teacher2 = new Teacher();  
  41.         teacher2.setId(2);  
  42.         teacher2.setName("丁老師");  
  43.         teacher2.setTitle("講師");  
  44.         List<Teacher> teacherList = new ArrayList<Teacher>();  
  45.         teacherList.add(teacher1);  
  46.         teacherList.add(teacher2);  
  47.   
  48.         TableData td1 = new TableData();  
  49.         td1.setTableName("students");  
  50.         td1.setTableData(stulist);  
  51.   
  52.         TableData td2 = new TableData();  
  53.         td2.setTableName("teachers");  
  54.         td2.setTableData(teacherList);  
  55.   
  56.         List<TableData> tdList = new ArrayList<TableData>();  
  57.         tdList.add(td1);  
  58.         tdList.add(td2);  
  59.         Gson gson = new Gson();  
  60.         String s = gson.toJson(tdList);  
  61.   
  62.         System.out.println(s);  
  63.         // 結果:[{"tableName":"students","tableData":[{"id":1,"name":"李坤","birthDay":"Jun 22, 2012 10:44:16 AM"},{"id":2,"name":"曹貴生","birthDay":"Jun 22, 2012 10:44:16 AM"},{"id":3,"name":"柳波","birthDay":"Jun 22, 2012 10:44:16 AM"}]},{"tableName":"teachers","tableData":[{"id":1,"name":"米老師","title":"教授"},{"id":2,"name":"丁老師","title":"講師"}]}]  
  64.         // 對象轉爲Json-->end  
  65.   
  66.         // /////////////////////////////////////////////////////////////////////  
  67.   
  68.         // 將json轉爲數據-->start  
  69.         List<TableData> tableDatas2 = gson.fromJson(s,  
  70.                 new TypeToken<List<TableData>>() {  
  71.                 }.getType());  
  72.         for (int i = 0; i < tableDatas2.size(); i++) {  
  73.             TableData entityData = tableDatas2.get(i);  
  74.             String tableName = entityData.getTableName();  
  75.             List tableData = entityData.getTableData();  
  76.             String s2 = gson.toJson(tableData);  
  77.             // System.out.println(s2);  
  78.             // System.out.println(entityData.getData());  
  79.             if (tableName.equals("students")) {  
  80.                 System.out.println("students");  
  81.                 List<Student> retStuList = gson.fromJson(s2,  
  82.                         new TypeToken<List<Student>>() {  
  83.                         }.getType());  
  84.                 for (int j = 0; j < retStuList.size(); j++) {  
  85.                     System.out.println(retStuList.get(j));  
  86.                 }  
  87.   
  88.             } else if (tableName.equals("teachers")) {  
  89.                 System.out.println("teachers");  
  90.                 List<Teacher> retTchrList = gson.fromJson(s2,  
  91.                         new TypeToken<List<Teacher>>() {  
  92.                         }.getType());  
  93.                 for (int j = 0; j < retTchrList.size(); j++) {  
  94.                     System.out.println(retTchrList.get(j));  
  95.                 }  
  96.             }  
  97.         }  
  98.   
  99.         // Json轉爲對象-->end  
  100.     }  
  101. }  

輸出結果:

[plain] view plain copy
  1. [{"tableName":"students","tableData":[{"id":1,"name":"李坤","birthDay":"Jun 22, 2012 10:04:12 PM"},{"id":2,"name":"曹貴生","birthDay":"Jun 22, 2012 10:04:12 PM"},{"id":3,"name":"柳波","birthDay":"Jun 22, 2012 10:04:12 PM"}]},{"tableName":"teachers","tableData":[{"id":1,"name":"米老師","title":"教授"},{"id":2,"name":"丁老師","title":"講師"}]}]  
  2. students  
  3. Student [birthDay=Fri Jun 22 22:04:12 CST 2012, id=1, name=李坤]  
  4. Student [birthDay=Fri Jun 22 22:04:12 CST 2012, id=2, name=曹貴生]  
  5. Student [birthDay=Fri Jun 22 22:04:12 CST 2012, id=3, name=柳波]  
  6. teachers  
  7. Teacher [id=1, name=米老師, title=教授]  
  8. Teacher [id=2, name=丁老師, title=講師]  

  9. 轉自:http://blog.csdn.net/lk_blog/article/category/1172246
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章