我有如下的json响应:
{"IsValid":false,"ModelErrors":null,"ValidationErrors":[10000]}
型号类别:
public class ShipmentResponse {
private boolean isValid;
private ModelErrors modelErrors;
private List<Integer> validationErrors = null;
对象映射器代码:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ShipmentResponse shipmentResponse = mapper.readValue((BufferedInputStream)response.getEntity(), ShipmentResponse.class);
解析后,我无法将ValidationErrors从json映射到java,即,validationErrors = null。我期望validationErrors = {1000},但不确定为什么吗?我知道我们可以使用TypeReference返回数组或列表,但不能嵌套在数据对象内部。
最佳答案
尝试这个
public class ShipmentResponse {
@JsonProperty("IsValid")
private boolean isValid;
@JsonProperty("ModelErrors")
private ModelErrors modelErrors;
@JsonProperty("ValidationErrors")
private List<Integer> validationErrors = null;
}
通常,您的属性名称和实际的json不匹配(大小写有关)