gpt4 book ai didi

java - JsonParseException : Fail to Deserializer JSON

转载 作者:行者123 更新时间:2023-12-01 23:30:28 26 4
gpt4 key购买 nike

尝试反序列化此 JSON 字符串时出现以下异常:

{ "studentName": "John", "studentAge": "20" }

异常(exception):

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)

这些是我的类(class):

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);
}

类(class)信息.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。具体来说,JSON 中的 studentList 值是一个对象:

{
"studentName": "John",
"studentAge": "20"
}

但是,您正在尝试将该对象读入列表中。鉴于该变量名为 studentList,我猜想 JSON 是错误的,而不是您的代码,它应该是一个数组:

{
"className": "Math",
"classTime": "2013-01-01 11:00",
"studentList": [
{
"studentName": "John",
"studentAge": "20"
}
]
}

关于java - JsonParseException : Fail to Deserializer JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19395229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com