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
发布了74 篇原创文章 · 获赞 58 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/Jason_996/article/details/78029060