이 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);
}
CourseInfo.java :
public class CourseInfo implements Serializable {
private static final long serialVersionUID = 1L;
private String className;
private Timestamp classTime;
private List<StudentInfo> studentList;
...
}
StudentInfo.java
public class CourseInfo implements Serializable {
private static final long serialVersionUID = 1L;
private String studentName;
private String studentAge;
...
}
읽으려는 객체와 일치하지 않는 일부 JSON을 읽으려고합니다. 특히 studentList
JSON 의 값은 객체입니다.
{
"studentName": "John",
"studentAge": "20"
}
그러나 해당 개체를 목록으로 읽으려고합니다. 변수의 이름 studentList
이이면 JSON이 코드가 아니라 잘못되었으며 대신 배열이어야한다고 생각합니다.
{
"className": "Math",
"classTime": "2013-01-01 11:00",
"studentList": [
{
"studentName": "John",
"studentAge": "20"
}
]
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다