无法从 VALUE_STRING token [...] 中反序列化 java.lang.String[] 的实例(通过引用链:[...].model.User["ethnicities"])
我有一个带有属性ethnicities
的用户对象。
据我所知,错误是因为 json“类型”与属性类型不匹配,但在我的情况下,两者都是字符串数组。
我做错了什么吗?
此外,我无法弄清楚 VALUE_STRING
在解析中代表什么。
服务器端使用 Loopback,并且种族属性定义如下:
“种族”:[
””
]
Android 客户端使用 Jackson 将 json 映射到 pojo,反之亦然。 pojo 中的种族如下所示:
private String[] ethnicities;
public String[] getEthnicities() {return ethnicities;}
public void setEthnicities(String[] ethnicities) {this.ethnicities = ethnicities;}
到 json 的映射:
try {
ObjectMapper mapper = new ObjectMapper();
jsonUserObject = mapper.writeValueAsString(user);
} catch (IOException e) {
System.out.println("IOException converting user object to JSON");
}
以及来自 json 的映射:
try {
ObjectMapper mapper = new ObjectMapper();
user = mapper.readValue(userObjectJSON, User.class);
} catch (IOException e) {
System.out.println("IOException converting JSON to user object");
}
以下是传入 json 的一些示例:
"ethnicities": [
"Asian",
"American Indian",
"Hispanic"
]
"ethnicities": [
"Caucasian"
]
"ethnicities": null
谢谢!
最佳答案
尝试将此标志设置为ObjectMapper
:
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
关于java - 无法从 VALUE_STRING token 中反序列化 java.lang.String[] 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43131982/