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

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串.请看本文是如何处理的吧:

?

实体类:

?

?

?

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

?