1. 程式人生 > >解決com.fasterxml.jackson.databind.JsonMappingException: No suitable 以 Jackson 處理複雜型別(List,map)兩種方法

解決com.fasterxml.jackson.databind.JsonMappingException: No suitable 以 Jackson 處理複雜型別(List,map)兩種方法

出現com.fasterxml.jackson.databind.JsonMappingException: No suitable 的情況有很多種,具體情況具體對待。

原因一:

在使用jackson 進行json字串轉java bean的時候出現如下錯誤,

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class org.test.Library]: can not instantiate from JSON object (need to add/enable type information?)  at [Source: java.io.StringReader@d1831; line: 1, column: 2]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:983) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:276) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)

原因java bean中沒有提供無參的建構函式,如下

public class Library {  
 
    private String name;  
       
    private List<Book> bookList ;  
    //新增無參的構造器
    public Library(){ 
    }
     
    public Library(String name){
    this.name = name;  
    }
     
    public String getName() {  
        return name;  
    } 
    public void setName(String name) {  
        this.name = name;  
    }
 
    public List<Book> getBookList() {  
        return bookList;  
    }  
   
    public void setBookList(List<Book> bookList) {  
        this.bookList = bookList;  
    }
}

第二種情況:

dataListentity  json2=[{"num":122,"red":"豔紅色的22","green":"淡綠色的22"}]
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of text.TextEntity out of START_ARRAY token
 at [Source: [{"num":122,"red":"豔紅色的22","green":"淡綠色的22"}]; line: 1, column: 1]
	at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
	at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:762)
	at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:758)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1211)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:147)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:126)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3066)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2161)

原因:Can not deserialize instance of text.TextEntity out of START_ARRAY token這一句基本可以斷定是類資訊不能被反序列化。所以就需要用到下面的方法進行處理複雜型別(List,map)

方法一:

String cols="[{'id':'3'},{'id':'3'}]";
ObjectMapper mapper = new ObjectMapper();
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, Bean.class);
//Map型別  
mapper.getTypeFactory().constructParametricType(HashMap.class,String.class, Bean.class);
List<Bean> lst =  (List<Bean>)mapper.readValue(cols, javaType); 



方法二:

String jsonString="[{'id':'1'},{'id':'2'}]";
ObjectMapper mapper = new ObjectMapper();
List<Bean> beanList = mapper.readValue(jsonString, new TypeReference<List<Bean>>() {});