前面四篇博客基本上可以满足我们处理的绝大多数需求,但有时项目中对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串.请看本文是如何处理的吧:
?
实体类:
?
?
?
- import?java.util.Date;??
- ??
- public?class?Student?{??
- ????private?int?id;??
- ????private?String?name;??
- ????private?Date?birthDay;??
- ??
- ????public?int?getId()?{??
- ????????return?id;??
- ????}??
- ??
- ????public?void?setId(int?id)?{??
- ????????this.id?=?id;??
- ????}??
- ??
- ????public?String?getName()?{??
- ????????return?name;??
- ????}??
- ??
- ????public?void?setName(String?name)?{??
- ????????this.name?=?name;??
- ????}??
- ??
- ????public?Date?getBirthDay()?{??
- ????????return?birthDay;??
- ????}??
- ??
- ????public?void?setBirthDay(Date?birthDay)?{??
- ????????this.birthDay?=?birthDay;??
- ????}??
- ??
- ????@Override??
- ????public?String?toString()?{??
- ????????return?"Student?[birthDay="?+?birthDay?+?",?id="?+?id?+?",?name="??
- ????????????????+?name?+?"]";??
- ????}??
- ??
- }??
?