Json转换利器Gson之实例四-Map处理(下)

Map的存储结构式Key/Value形式,Key 和 Value可以是普通类型,也可以是自己写的JavaBean(上一篇博客),还可以是带有泛型的List(本文).本例中您要重点看如何将Json转回为带泛型的对象List,并且List中的泛型对象有多种实体.

实体类:

[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. }

测试类:

[java][/java]

view plaincopy

  1. package com.tgb.lk.demo.gson.test4;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.LinkedHashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import com.google.gson.Gson;
  8. import com.google.gson.reflect.TypeToken;
  9. public class GsonTest4 {
  10.     public static void main(String[] args) {
  11.         Student student1 = new Student();
  12.         student1.setId(1);
  13.         student1.setName(“李坤”);
  14.         student1.setBirthDay(new Date());
  15.         Student student2 = new Student();
  16.         student2.setId(2);
  17.         student2.setName(“曹贵生”);
  18.         student2.setBirthDay(new Date());
  19.         Student student3 = new Student();
  20.         student3.setId(3);
  21.         student3.setName(“柳波”);
  22.         student3.setBirthDay(new Date());
  23.         List<Student> stulist = new ArrayList<Student>();
  24.         stulist.add(student1);
  25.         stulist.add(student2);
  26.         stulist.add(student3);
  27.         Teacher teacher1 = new Teacher();
  28.         teacher1.setId(1);
  29.         teacher1.setName(“米老师”);
  30.         teacher1.setTitle(“教授”);
  31.         Teacher teacher2 = new Teacher();
  32.         teacher2.setId(2);
  33.         teacher2.setName(“丁老师”);
  34.         teacher2.setTitle(“讲师”);
  35.         List<Teacher> teacherList = new ArrayList<Teacher>();
  36.         teacherList.add(teacher1);
  37.         teacherList.add(teacher2);
  38.         Map<String, Object> map = new LinkedHashMap<String, Object>();
  39.         map.put(“students”, stulist);
  40.         map.put(“teachers”, teacherList);
  41.         Gson gson = new Gson();
  42.         String s = gson.toJson(map);
  43.         System.out.println(s);
  44.         System.out.println(“———————————-“);
  45.         Map<String, Object> retMap = gson.fromJson(s,
  46.                 new TypeToken<Map<String, List<Object>>>() {
  47.                 }.getType());
  48.         for (String key : retMap.keySet()) {
  49.             System.out.println(“key:” + key + ” values:” + retMap.get(key));
  50.             if (key.equals(“students”)) {
  51.                 List<Student> stuList = (List<Student>) retMap.get(key);
  52.                 System.out.println(stuList);
  53.             } else if (key.equals(“teachers”)) {
  54.                 List<Teacher> tchrList = (List<Teacher>) retMap.get(key);
  55.                 System.out.println(tchrList);
  56.             }
  57.         }
  58.     }
  59. }

输出结果:

[plain][/plain]

view plaincopy

  1. {“students”:[{“id”:1,”name”:”李坤”,”birthDay”:”Jun 22, 2012 9:48:19 PM”},{“id”:2,”name”:”曹贵生”,”birthDay”:”Jun 22, 2012 9:48:19 PM”},{“id”:3,”name”:”柳波”,”birthDay”:”Jun 22, 2012 9:48:19 PM”}],”teachers”:[{“id”:1,”name”:”米老师”,”title”:”教授”},{“id”:2,”name”:”丁老师”,”title”:”讲师”}]}
  2. ———————————-
  3. key:students values:[{id=1.0, name=李坤, birthDay=Jun 22, 2012 9:48:19 PM}, {id=2.0, name=曹贵生, birthDay=Jun 22, 2012 9:48:19 PM}, {id=3.0, name=柳波, birthDay=Jun 22, 2012 9:48:19 PM}]
  4. [{id=1.0, name=李坤, birthDay=Jun 22, 2012 9:48:19 PM}, {id=2.0, name=曹贵生, birthDay=Jun 22, 2012 9:48:19 PM}, {id=3.0, name=柳波, birthDay=Jun 22, 2012 9:48:19 PM}]
  5. key:teachers values:[{id=1.0, name=米老师, title=教授}, {id=2.0, name=丁老师, title=讲师}]
  6. [{id=1.0, name=米老师, title=教授}, {id=2.0, 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

标签