2016-03-03 41 views
0

我有這樣的JSON:傑克遜JSON API反序列化流無法識別的現場

[{"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它的作品,但我不想在哪個整數字段。

回答

3

這聽起來更像是命名約定不匹配。 setNrParcela將映射到字段名稱nrParcela,但您的JSON文檔的'n'大寫爲NrParcela

如果你不能改變JSON場資本,你可以使用@JsonProperty與重寫名稱:

@JsonProperty("NrParcela") 

但既然你不想這樣做,另一個值得考慮的選擇是實施PropertyNamingStrategy

+0

Omg。我不相信我沒有看到那個kkk。謝謝。它起作用^^ – Cechinel