日期:2014-05-16  浏览次数:20430 次

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

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

?

?

实体类:

?

?

?

  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;??