使用Java生成json数据
1.导入相关jar包
2.定义vo操作类
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{"+"name="+name+",age"+age+"}";
}
}
生成toString是为了方便观察
3定义测试类
public class TestDemo {
public static void main(String[] args) {
JSONObject obj = new JSONObject();
JSONArray array = new JSONArray();
for(int i = 0; i <5 ; i++) {
User vo = new User();
vo.setName("测试数据"+"1000"+i);
vo.setAge(15+i);
array.put(vo);
}
obj.put("allUser",array);
System.out.println(obj);
}
}
生成结果
{"allUser":["User{name=测试数据10000,age15}","User{name=测试数据10001,age16}","User{name=测试数据10002,age17}","User{name=测试数据10003,age18}","User{name=测试数据10004,age19}"]}