使用 ModelMapper 反序列化 Json 时如何修复错误 START_ARRAY 令牌?
How to fix Error START_ARRAY token when deserializing Json with ModelMapper?
我正在尝试以这种格式反序列化 JSON 文件:
[
["AA", "GG", "1992/11/18"],
["BB", "DD", "2005/02/20"]
]
使用这个 class:
public class DataList {
private List<String> att;
// constructor, getter and setter
}
正在做:
DataList [] dataList= mapper.readValue(ResourceUtils.getFile("classpath:" + filename), DataList [].class);
但我得到:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `org.example.DataList ` out of START_ARRAY token
at [Source: (File); line: 2, column: 3] (through reference chain: java.lang.Object[][0])
知道如何解决这个错误吗?
Jackson 不知道如何将字符串数组映射到 DataList
对象。因此,您应该在 DataList
构造函数上添加 @JsonCreate
以向 Jackson 展示用于转换的内容。
public class DataList {
private List<String> att;
@JsonCreator
public DataList(List<String> att) {
this.att = att;
}
// constructor, getter and setter
}
我正在尝试以这种格式反序列化 JSON 文件:
[
["AA", "GG", "1992/11/18"],
["BB", "DD", "2005/02/20"]
]
使用这个 class:
public class DataList {
private List<String> att;
// constructor, getter and setter
}
正在做:
DataList [] dataList= mapper.readValue(ResourceUtils.getFile("classpath:" + filename), DataList [].class);
但我得到:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `org.example.DataList ` out of START_ARRAY token
at [Source: (File); line: 2, column: 3] (through reference chain: java.lang.Object[][0])
知道如何解决这个错误吗?
Jackson 不知道如何将字符串数组映射到 DataList
对象。因此,您应该在 DataList
构造函数上添加 @JsonCreate
以向 Jackson 展示用于转换的内容。
public class DataList {
private List<String> att;
@JsonCreator
public DataList(List<String> att) {
this.att = att;
}
// constructor, getter and setter
}