我有这个 json:
[{"cdCondicaoPagto":"1","NrParcela":1,"NrDias":0}]
这个类:
public static class CondicaoPagtoItem implements Serializable {
private String cdCondicaoPagto;
private Integer NrParcela;
private Integer NrDias;
public CondicaoPagtoItem() {
}
public String getCdCondicaoPagto() {
return cdCondicaoPagto;
}
public void setCdCondicaoPagto(String cdCondicaoPagto) {
this.cdCondicaoPagto = cdCondicaoPagto;
}
public Integer getNrParcela() {
return NrParcela;
}
public void setNrParcela(Integer NrParcela) {
this.NrParcela = NrParcela;
}
public Integer getNrDias() {
return NrDias;
}
public void setNrDias(Integer NrDias) {
this.NrDias = NrDias;
}
}
我正在尝试通过流式传输的方式阅读它:
JsonFactory jsonFactory = new JsonFactory();
ObjectMapper jsonMapper = new ObjectMapper(jsonFactory);
JsonNode jsonNodeGeral = jsonMapper.readTree(new File("/home/cechinel/Documentos/CondicaoPagtoItem.json"));
Iterator<JsonNode> elements = jsonNodeGeral.getElements();
while(elements.hasNext()){
JsonNode jsonNode = elements.next();
CondicaoPagtoItem condicao = jsonMapper.treeToValue(jsonNode, CondicaoPagtoItem.class);
}
但它导致以下错误:
UnrecognizedPropertyException: Unrecognized field "NrParcela"
如果我使用注释 @JsonProperty 它可以工作,但我不想在哪个整数字段中执行它。
最佳答案
在我看来,这更像是命名约定不匹配。 setNrParcela
将映射到字段名称 nrParcela
,但您的 JSON 文档中的“n”大写为 NrParcela
。
如果您无法更改 JSON 字段大小写,则可以使用带有覆盖名称的 @JsonProperty
:
@JsonProperty("NrParcela")
但是由于您不想这样做,因此可以考虑的另一个选择是实现 PropertyNamingStrategy .
关于java - Jackson JSON API反序列化流无法识别的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35783299/