簡體   English   中英

Jackson JSON API反序列化流無法識別的字段

[英]Jackson JSON API Deserialization Streaming Unrecognized Field

我有這個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:無法識別的字段“ NrParcela”

如果我使用注釋@JsonProperty,它可以工作,但是我不想在整數字段中使用它。

在我看來,這更像是命名約定不匹配。 setNrParcela會映射到字段名稱nrParcela但是您的JSON文檔的“ n”大寫為NrParcela

如果您無法更改JSON字段的大小寫,則可以使用@JsonProperty並使用覆蓋名稱:

@JsonProperty("NrParcela")

但是由於您不想這樣做,因此要考慮的另一種選擇是實現PropertyNamingStrategy

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM