我的任务是描述类Student和Academic组(使用字段-学生数组)。创建对象。使用三种方法提供文件创建和读取:
JSON中的序列化和反序列化(通过XStream)。
我序列化一个有学生数组列表的学术组:
public class AcademicGroup {
private List<Student> students;
public AcademicGroup() {
this.students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public Student[] getStudents() {
return students.toArray(new Student[0]);
}
}
和学生:
public class Student {
private String name;
private String group;
public Student(String name, String group) {
this.name = name;
this.group = group;
}
public String getName() {
return name;
}
public String getGroup() {
return group;
}
@Override
public String toString() {
return name + " " + group;
}
}
序列化函数public static void studentJsonSerialization() {
XStream xStream = new XStream(new JsonHierarchicalStreamDriver());
List<Student> students = Arrays.asList(
new Student("Student1", "KN-221A"),
new Student("Student2", "KN-221A"),
new Student("Student3", "KN-221A")
);
AcademicGroup academicGroup = new AcademicGroup();
for (Student student : students) {
academicGroup.addStudent(student);
}
xStream.alias("academicGroup", AcademicGroup.class);
String xml = xStream.toXML(academicGroup);
try (FileWriter fw = new FileWriter("AcademicGroup.json");
PrintWriter out = new PrintWriter(fw)) {
out.println(xml);
}
catch (IOException e) {
e.printStackTrace();
}
}
文件AcademicGroup。我得到:
{"academicGroup": {
"students": [
{
"name": "Student1",
"group": "KN-221A"
},
{
"name": "Student2",
"group": "KN-221A"
},
{
"name": "Student3",
"group": "KN-221A"
}
]
}}
反序列化功能:
public static void studentJsonDeserialization() {
XStream xStream = new XStream(new JettisonMappedXmlDriver());
xStream.addPermission(AnyTypePermission.ANY);
xStream.alias("academicGroup", AcademicGroup.class);
AcademicGroup newAcademicGroup = (AcademicGroup) xStream.fromXML(new File("AcademicGroup.json"));
for (Student student : newAcademicGroup.getStudents()) {
System.out.println(student.toString());
}
}
我需要在控制台打印json对象,但我得到一个错误消息
Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException:
---- Debugging information ----
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : name
class : java.util.ArrayList
required-type : java.util.ArrayList
converter-type : com.thoughtworks.xstream.converters.collections.CollectionConverter
path : /academicGroup/students/name
line number : -1
class[1] : org.example.AcademicGroup
required-type[1] : org.example.AcademicGroup
converter-type[1] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version : 1.4.20
你试着添加xStream.autodetectAnnotations(true). 这应该可以工作。
public static void studentJsonDeserialization() {
XStream xStream = new XStream(new JettisonMappedXmlDriver());
xStream.autodetectAnnotations(true);
xStream.addPermission(AnyTypePermission.ANY);
xStream.alias("academicGroup", AcademicGroup.class);
AcademicGroup newAcademicGroup = (AcademicGroup) xStream.fromXML(new File("AcademicGroup.json"));
for (Student student : newAcademicGroup.getStudents()) {
System.out.println(student.toString());
}
}