json转dto
2021-07-10 本文已影响0人
蛋皮皮652
JSON与List转换
fastJson
//list -> json
List<Student> list = new ArrayList<Student>();
String str=JSON.toJSON(list).toString();
//json -> list
List<Student> list = new ArrayList<Student>();
list = JSONObject.parseArray(jasonArray, Student.class);
Gson
//list -> json
Gson gson = new Gson();
List<Student> students = new ArrayList<Student>();
String str = gson.toJson(students);
//json -> list
Gson gson = new Gson();
List<Student> students = gson.fromJson(str, new TypeToken<List<Student>>(){}.getType());
JsonObject obj = JSON.parseObject(res);
List<StudentDto> nodeAttrs = new Gson().fromJson(obj.get("data"), new TypeToken<List<StudentDto>>(){}.getType());