尝试反序列化此 JSON 字符串时出现以下异常:
{ "studentName": "John", "studentAge": "20" }
例外:
com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@41d241d2 failed to deserialize json object { "studentName": "John", "studentAge": "20" } given the type java.util.List<...>
at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:64)
at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:92)
这些是我的课:
public class School {
Gson gson = new Gson();
String json = ...// I can read json from text file, the string is like { "className": "Math", "classTime": "2013-01-01 11:00", "studentList": { "studentName": "John", "studentAge": "20" }}
CourseInfo bean = gson.fromJson(json, CourseInfo.class);
}
课程信息.java:
public class CourseInfo implements Serializable {
private static final long serialVersionUID = 1L;
private String className;
private Timestamp classTime;
private List<StudentInfo> studentList;
...
}
学生信息.java
public class CourseInfo implements Serializable {
private static final long serialVersionUID = 1L;
private String studentName;
private String studentAge;
...
}