2017-06-22 222 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