定义teacher和student实体
json.do
List<Student> stus=new ArrayList<Student>();
stus.add(new Student("zsf1"));
stus.add(new Student("zsf2"));
stus.add(new Student("zsf3"));
stus.add(new Student("zsf4"));
Teacher teacher=new Teacher();
teacher.setName("chj");
teacher.setAge(28);
teacher.setBirth(new Date(1988,1,12));
teacher.setSex("男");
teacher.setStudents(stus);
JSONObject json=JSONObject.fromObject(teacher);
response.getOutputStream().write(json.toString().getBytes());
response.setContentType("application/json");
结果如下
{"age":28,"birth":{"date":12,"day":0,"hours":0,"minutes":0,"month":1,"seconds":0,"time":60529824000000,"timezoneOffset":-480,"year":1988},"name":"chj","sex":"男",
"students":[{"name":"zsf1"},{"name":"zsf2"},{"name":"zsf3"},{"name":"zsf4"}]}