Student s1 = new Student();
s1.setName("張三");
s1.setAge("18");
Student s2 = new Student();
s2.setName("李四");
s2.setAge("22");
List s3 = new ArrayList();
s3.add(0,s1);
s3.add(0,s2);
如何將 s3 轉換成json格式。
采用jackson。很方便就能實現你要的效果。
ObjectMapper mapper = new ObjectMapper();
String jsonStr = mapper.writeValueAsString(s3);