Json转换利器Gson之实例五-实际开发中的特殊需求处理

前面四篇博客基本上可以满足我们处理的绝大多数需求,但有时项目中对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][/java]

view plaincopy

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

view plaincopy

  1. public class Teacher {
  2.     private int id;
  3.     private String name;
  4.     private String title;
  5.     public int getId() {
  6.         return id;
  7.     }
  8.     public void setId(int id) {
  9.         this.id = id;
  10.     }
  11.     public String getName() {
  12.         return name;
  13.     }
  14.     public void setName(String name) {
  15.         this.name = name;
  16.     }
  17.     public String getTitle() {
  18.         return title;
  19.     }
  20.     public void setTitle(String title) {
  21.         this.title = title;
  22.     }
  23.     @Override
  24.     public String toString() {
  25.         return “Teacher [id=” + id + “, name=” + name + “, title=” + title
  26.                 + “]”;
  27.     }
  28. }

注意这里定义了一个TableData实体类:

[java][/java]

view plaincopy

  1. import java.util.List;
  2. public class TableData {
  3.     private String tableName;
  4.     private List tableData;
  5.     public String getTableName() {
  6.         return tableName;
  7.     }
  8.     public void setTableName(String tableName) {
  9.         this.tableName = tableName;
  10.     }
  11.     public List getTableData() {
  12.         return tableData;
  13.     }
  14.     public void setTableData(List tableData) {
  15.         this.tableData = tableData;
  16.     }
  17. }

测试类:

(仔细看将json转回为对象的实现,这里经过两次转化,第一次转回的结果是map不是我们所期望的对象,对map再次转为json后再转为对象,我引用的是Gson2.1的jar处理正常,好像使用Gson1.6的jar会报错,所以建议用最新版本)

[java][/java]

view plaincopy

  1. import java.util.ArrayList;
  2. import java.util.Date;
  3. import java.util.List;
  4. import com.google.gson.Gson;
  5. import com.google.gson.reflect.TypeToken;
  6. public class GsonTest5 {
  7.     /**
  8.      * @param args
  9.      */
  10.     public static void main(String[] args) {
  11.         // 对象转为Json–>start
  12.         Student student1 = new Student();
  13.         student1.setId(1);
  14.         student1.setName(“李坤”);
  15.         student1.setBirthDay(new Date());
  16.         Student student2 = new Student();
  17.         student2.setId(2);
  18.         student2.setName(“曹贵生”);
  19.         student2.setBirthDay(new Date());
  20.         Student student3 = new Student();
  21.         student3.setId(3);
  22.         student3.setName(“柳波”);
  23.         student3.setBirthDay(new Date());
  24.         List<Student> stulist = new ArrayList<Student>();
  25.         stulist.add(student1);
  26.         stulist.add(student2);
  27.         stulist.add(student3);
  28.         Teacher teacher1 = new Teacher();
  29.         teacher1.setId(1);
  30.         teacher1.setName(“米老师”);
  31.         teacher1.setTitle(“教授”);
  32.         Teacher teacher2 = new Teacher();
  33.         teacher2.setId(2);
  34.         teacher2.setName(“丁老师”);
  35.         teacher2.setTitle(“讲师”);
  36.         List<Teacher> teacherList = new ArrayList<Teacher>();
  37.         teacherList.add(teacher1);
  38.         teacherList.add(teacher2);
  39.         TableData td1 = new TableData();
  40.         td1.setTableName(“students”);
  41.         td1.setTableData(stulist);
  42.         TableData td2 = new TableData();
  43.         td2.setTableName(“teachers”);
  44.         td2.setTableData(teacherList);
  45.         List<TableData> tdList = new ArrayList<TableData>();
  46.         tdList.add(td1);
  47.         tdList.add(td2);
  48.         Gson gson = new Gson();
  49.         String s = gson.toJson(tdList);
  50.         System.out.println(s);
  51.         // 结果:[{“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”:”讲师”}]}]
  52.         // 对象转为Json–>end
  53.         // /////////////////////////////////////////////////////////////////////
  54.         // 将json转为数据–>start
  55.         List<TableData> tableDatas2 = gson.fromJson(s,
  56.                 new TypeToken<List<TableData>>() {
  57.                 }.getType());
  58.         for (int i = 0; i < tableDatas2.size(); i++) {
  59.             TableData entityData = tableDatas2.get(i);
  60.             String tableName = entityData.getTableName();
  61.             List tableData = entityData.getTableData();
  62.             String s2 = gson.toJson(tableData);
  63.             // System.out.println(s2);
  64.             // System.out.println(entityData.getData());
  65.             if (tableName.equals(“students”)) {
  66.                 System.out.println(“students”);
  67.                 List<Student> retStuList = gson.fromJson(s2,
  68.                         new TypeToken<List<Student>>() {
  69.                         }.getType());
  70.                 for (int j = 0; j < retStuList.size(); j++) {
  71.                     System.out.println(retStuList.get(j));
  72.                 }
  73.             } else if (tableName.equals(“teachers”)) {
  74.                 System.out.println(“teachers”);
  75.                 List<Teacher> retTchrList = gson.fromJson(s2,
  76.                         new TypeToken<List<Teacher>>() {
  77.                         }.getType());
  78.                 for (int j = 0; j < retTchrList.size(); j++) {
  79.                     System.out.println(retTchrList.get(j));
  80.                 }
  81.             }
  82.         }
  83.         // Json转为对象–>end
  84.     }
  85. }

输出结果:

[plain][/plain]

view plaincopy

  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=讲师]

实例代码下载: http://download.csdn.net/detail/lk_blog/4387822

 

相关文章推荐:

Json转换利器Gson之实例一-简单对象转化和带泛型的List转化

http://qiniu.devba.com/index.php/archives/1702.html
Json转换利器Gson之实例二-Gson注解和GsonBuilder

http://qiniu.devba.com/index.php/archives/1703.html

Json转换利器Gson之实例三-Map处理(上)

http://qiniu.devba.com/index.php/archives/1704.html
Json转换利器Gson之实例四-Map处理(下)

http://qiniu.devba.com/index.php/archives/1705.html
Json转换利器Gson之实例五-实际开发中的特殊需求处理

http://qiniu.devba.com/index.php/archives/1706.html
Json转换利器Gson之实例六-注册TypeAdapter及处理Enum类型

http://qiniu.devba.com/index.php/archives/1707.html

标签