2017-06-22 108 views
1

我打電話給一個API,結果我能夠得到Json響應。在這個json中,我有一個參數manufacturer其中我期待有一個類型爲Manufacturer(我的自定義類)的對象,但有一段時間我從相同的命名參數獲取String到API中,由於這個原因,我能夠得到以下異常:Android傑克遜映射異常

com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.response.Manufacturer] from String value ('59438a9f2e4ab8c26656fcfd'); no single-String constructor/factory. 
這我得到

API響應是這樣的:

{"manufacturer" : "59438a9f2e4ab8c26656fcfd"} 

並且有時這種反應來像

{"manufacturer" : {"name":"Manufacturer Name"}} 

我製造商類似於

class Manufacturer { 
@JsonProperty("name") 
private String name; 
    // getter and setter for this with @JsonProperty("name"); 
} 

我已經在谷歌搜索了很多,不能找到任何解決方案。請幫我解決這個問題。

回答

0

你可以把一個構造帶String論點Manufacturer當它看到了JSON字符串字段,並在Java類創建Manufacturer場,傑克遜將使用:

class Manufacturer { 
    Manufacturer(String id) { 
     this.name = // initialize as you see fit 
    } 

    Manufacturer() {} // compiler no longer creates this