大家好,又见面了,我是你们的朋友全栈君。
三种转换方法
String courseData = "...";
List<Course> courseTable;
courseTable = JSON.parseObject(courseData, new TypeReference<List<Course>>() {});
courseTable = JSON.parseArray(courseData, Course.class);
courseTable = JSON.parseArray(courseData).toJavaList(Course.class);
System.out.println(courseTable);
其中,第三种方法:
Code
// List
List<Course> courseTable = new LinkedList<>();
courseTable.add(new Course("1", "Java", 30, 3.0));
courseTable.add(new Course("2", "JavaScript", 25, 2.5));
System.out.println(courseTable);
// List -> String
String str = JSON.toJSONString(courseTable);
System.out.println(str);
// String -> JSONArray
JSONArray objects = JSON.parseArray(str);
System.out.println(objects);
// JSONArray -> List
List<Course> courses = objects.toJavaList(Course.class);
System.out.println(courses);
输出
[{"course":"Java","credit":3.0,"id":"1","number":30}, {"course":"JavaScript","credit":2.5,"id":"2","number":25}]
[{"course":"Java","credit":3.0,"id":"1","number":30},{"course":"JavaScript","credit":2.5,"id":"2","number":25}]
[{"number":30,"course":"Java","id":"1","credit":3.0},{"number":25,"course":"JavaScript","id":"2","credit":2.5}]
[{"course":"Java","credit":3.0,"id":"1","number":30}, {"course":"JavaScript","credit":2.5,"id":"2","number":25}]
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/151458.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...