JSONArray

List<User> user = new ArrayList<User>();
User u1 = new User();
u1.setId(110l);
u1.setUserName("张三");
user.add(u1);
u1 = new User();
u1.setId(112l);
u1.setUserName("李四");
user.add(u1);
u1 = new User();
u1.setId(113l);
u1.setUserName("王五");
try{
JSONArray array = new JSONArray();
for(User u : user){
JSONObject o = new JSONObject();
o.put("id", u.getId());
o.put("userName", u.getUserName());
array.put(o);
}
System.out.println(array.toString());
}catch(Exception e){
e.printStackTrace();
}
[{"id":110,"userName":"张三"},{"id":112,"userName":"李四"}]

你可能感兴趣的:(JsonArray)