三种转换方法
String courseData = "...";
List courseTable;
courseTable = JSON.parseObject(courseData, new TypeReference>() {});
courseTable = JSON.parseArray(courseData, Course.class);
courseTable = JSON.parseArray(courseData).toJavaList(Course.class);
System.out.println(courseTable);
其中,第三种方法:
Code
// List
List 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 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}]
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/148715.html