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

来源:互联网 发布:经传主力状态指标源码 编辑:程序博客网 时间:2024/04/29 09:19

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

实体类:

[java] view plaincopy
  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 plaincopy
  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. }  

测试类:

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

输出结果:

[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=讲师}]  
0 0
原创粉丝点击