我以前从未使用过JSON,因此想将ArrayList<Person>
序列化为JSON文件。
我的作家班看起来像这样:
public class Writer {
public void write(){
ArrayList<Person> personList = new ArrayList<>();
Person p1 = new Person("James", "Bond", LocalDate.of(1997,9,22));
Person p2 = new Person("Santa", "Claus", LocalDate.of(1918,11,6));
Person p3 = new Person("Peter", "Griffin", LocalDate.of(1978,3,24));
Person p4 = new Person("Lois", "Griffin", LocalDate.of(1982,7,14));
personList.add(p1);
personList.add(p2);
personList.add(p3);
personList.add(p4);
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
try {
writer.writeValue(new File(System.getProperty("user.dir")+"/File/Person.json"), personList);
} catch (IOException e) {
e.printStackTrace();
}
}
我的阅读器类如下:
public class Reader {
public void read(){
ObjectMapper mapper = new ObjectMapper();
try {
ArrayList<Person> liste = mapper.readValue(new FileInputStream("File/Personen.json"), ArrayList.class);
System.out.println("Read: " + liste.get(0));
} catch (IOException e) {
e.printStackTrace();
}
}
列表中的第一个对象如下所示:
Gelesen: {firstname=James, lastname=Bond, birthday={year=1997, month=SEPTEMBER, monthValue=9, dayOfMonth=22, chronology={id=ISO, calendarType=iso8601}, era=CE, dayOfYear=265, dayOfWeek=MONDAY, leapYear=false}}
如何将此JSON字符串转换回“ person”类的java对象? 我的序列化/反序列化有什么问题吗?
编辑:我想检查从JSON文件反序列化的列表中的我的人是否与原始人相同,因此我写了System.out.println(list.get(0).getFirstname()
,然后得到了java.lang.ClassCastException: java.base/java.util.LinkedHashMap cannot be cast to Person
答案 0 :(得分:1)
请您尝试以下代码。我认为您需要将您的数组列表转换为json格式。并以json格式检索。
public void write(){
ArrayList<Person> personList = new ArrayList<>();
JSONObject obj = new JSONObject();//JSON object.
Person p1 = new Person("James", "Bond", LocalDate.of(1997,9,22));
Person p2 = new Person("Santa", "Claus", LocalDate.of(1918,11,6));
Person p3 = new Person("Peter", "Griffin", LocalDate.of(1978,3,24));
Person p4 = new Person("Lois", "Griffin", LocalDate.of(1982,7,14));
personList.add(p1);
personList.add(p2);
personList.add(p3);
personList.add(p4);
obj.put("list", personList);
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
try {
writer.writeValue(new File(System.getProperty("user.dir")+"/File/Person.json"), obj);
} catch (IOException e) {
e.printStackTrace();
}
}
public void read(){
ObjectMapper mapper = new ObjectMapper();
try {
JSONObject obj = mapper.readValue(new File("G:\\Personen.json"));
// Convert JSON string from file to Object
Person person = mapper.readValue(new File("G:\\Personen.json"), Person.class);
System.out.println("Read: " + obj);
} catch (IOException e) {
e.printStackTrace();
}
}
答案 1 :(得分:1)
第一个问题:您要在不使用任何泛型类型的情况下反序列化到ArrayList,因此您将收到LinkedHashMap的ArrayList。杰克逊不知道您想要的人的名单,所以它使用LinkedHashMap作为适用于任何json的类型。
要反序列化到您应该使用的人员列表:
ArrayList<Person> list = mapper.readValue(new FileInputStream("./person.json"),
mapper.getTypeFactory().constructCollectionType(ArrayList.class, Person.class));
第二个问题:您正在使用Java 8时间(LocalDate类),但是Jackson对此一无所知。为了能够正确处理它,您需要从JavaTimeModule
依赖项中添加jackson-datatype-jsr310
并进行注册。
因此生成的代码将如下所示:
public void write() {
ArrayList<Person> personList = new ArrayList<>();
Person p1 = new Person("James", "Bond", LocalDate.of(1997, 9, 22));
Person p2 = new Person("Santa", "Claus", LocalDate.of(1918, 11, 6));
Person p3 = new Person("Peter", "Griffin", LocalDate.of(1978, 3, 24));
Person p4 = new Person("Lois", "Griffin", LocalDate.of(1982, 7, 14));
personList.add(p1);
personList.add(p2);
personList.add(p3);
personList.add(p4);
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
try {
writer.writeValue(new File("./person.json"), personList);
} catch (IOException e) {
e.printStackTrace();
}
}
public void read() {
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
try {
ArrayList<Person> list = mapper.readValue(new FileInputStream("./person.json"),
mapper.getTypeFactory().constructCollectionType(ArrayList.class, Person.class));
System.out.println("Read: " + list.get(0).getFirstname());
} catch (IOException e) {
e.printStackTrace();
}
}