我正在尝试从后端 api 调用映射一个数组。我如何映射这些数据知道:
以下类将用于保存 json 数组数据:
@Data
private static class UserListBean {
private List<UserBean> userList;
}
@Data
private static class UserBean {
private String id;
private String userName;
private String password;
}
json 将具有以下格式(以下示例中只有一项):
[
{
"id":1,
"userName":"bob",
"password":"403437d5c3f70b1329f37a9ecce02adbbf3e986"
}
]
我正在使用杰克逊,到目前为止我已经尝试了以下方法,但它不断向我发送异常
final ObjectMapper mapper = new ObjectMapper();
mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
final ObjectReader reader = mapper.reader(UserListBean.class);
GeoHubAccountListBean accounts = null;
try {
accounts = reader.readValue(jsonString);
} catch (final IOException ex) {
log.error("Cannot convert JSON into a list of users", ex);
}
这里最终的 ObjectReader reader = mapper.reader(UserListBean.class); 抛出异常
Can not deserialize instance of com.xxx.XXX$UserListBean out of START_ARRAY token
任何想法 ?
谢谢