【发布时间】:2017-08-25 05:05:16
【问题描述】:
无法从 VALUE_STRING 令牌 [...] 中反序列化 java.lang.String[] 的实例(通过引用链:[...].model.User["ethnicities"])
我有一个属性为ethnicities 的用户对象。
据我所知,错误是因为 json“类型”与属性类型不匹配,但在我的情况下两者都是字符串数组。
我做错了吗?
另外,我无法弄清楚VALUE_STRING 在解析中代表什么。
服务器端正在使用 Loopback,并且种族属性定义如下:
"ethnicities": [
""
]
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
谢谢!
【问题讨论】: