fastjson
public class JsonTest {
public static JSONObject fun(Object... objs){
JSONObject jsonObject = new JSONObject();
for (int i=0;i<objs.length;i+=2) {
jsonObject.put((String) objs[i],objs[i+1]);
}
return jsonObject;
}
public static void main(String[] args) {
Dept dept = DataUtil.build(Dept.class);
Dept dept1 = DataUtil.build(Dept.class);
Dept dept2 = DataUtil.build(Dept.class);
Dept dept3 = DataUtil.build(Dept.class);
List<Dept> deptList = List.of(dept1,dept2,dept3);
JSONObject jsonObject = fun("dept", dept, "deptList", deptList);
System.out.println(jsonObject);
}
}
jackson
public class JsonTest {
public static ObjectNode fun(Object... objs){
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode objectNode = objectMapper.createObjectNode();
for (int i=0;i<objs.length;i+=2) {
objectNode.putPOJO((String) objs[i],objs[i+1]);
}
return objectNode;
}
public static void main(String[] args) {
Dept dept = DataUtil.build(Dept.class);
Dept dept1 = DataUtil.build(Dept.class);
Dept dept2 = DataUtil.build(Dept.class);
Dept dept3 = DataUtil.build(Dept.class);
List<Dept> deptList = List.of(dept1,dept2,dept3);
ObjectNode objectNode = fun("dept", dept, "deptList", deptList);
System.out.println(objectNode);
}
}
程序最终结果
|