org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found
for
type [simple type,
class
test.jackson.Employee]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: c:\temp\employee.json; line:
1
, column:
2
]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:
163
)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:
483
)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:
350
)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:
2395
)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:
1549
)
at test.jackson.JSONToJavaExample.main(JSONToJavaExample.java:
19
)
一般來說,解決上面問題從下面幾個方面入手:
1、是否缺少默認構造函數
2、是否是類的訪問修飾符問題,即jackson訪問不到。
jackson 的UnrecognizedPropertyException錯誤
java代碼如下:
- public class Student implements Serializable{
- private static final long serialVersionUID = 685922460589405829L;
- private String name;
- private String age;
- /*get set略*/
- }
json字符串如下:
"{\"address\":\"hunan\",\"name\":\"china\",\"age\":\"5000\"}"
這種情況下,使用jackson從string轉換為object的時候,會拋出下面異常:
- java.lang.IllegalArgumentException: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "address" (Class util.Student), not marked as ignorable
意思是說Student類里沒有address這個屬性,所以無法正常轉化,同時還指明了not marked as ignorable,即沒有標明可忽略的特性
解決方案:
1、在類上添加忽略聲明
@JsonIgnoreProperties(ignoreUnknown=true)
2、更改str2obj方法
ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); projectVO = objectMapper.readValue(yourjsonstring, Test.class);
Jackson annotation - How to rename element names?
you can rename a property just adding
@JsonProperty("contractor")
And by default Jackson use the getter and setter to serialize and deserialize.
Using a custom JsonSerializer.
public class Response { private String status; private String error; @JsonProperty("p") @JsonSerialize(using = CustomSerializer.class) private Object data; // ... } public class CustomSerializer extends JsonSerializer<Object> { public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeStartObject(); jgen.writeObjectField(value.getClass().getName(), value); jgen.writeEndObject(); } }
Could not read JSON: Can not construct instance of java.util.Date from String value '2012-07-21 12:11:12': not a valid representation
- "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
- "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
- "EEE, dd MMM yyyy HH:mm:ss zzz"
- "yyyy-MM-dd"
當屍實體中存在Date類型,但是json字符串中是字符串類型
只支持以上幾種格式否則報錯
- /**
- * java日期對象經過Jackson庫轉換成JSON日期格式化自定義類
- * @author godfox
- * @date 2010-5-3
- */
- public class CustomDateSerializer extends JsonSerializer<Date> {
- @Override
- public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
- String formattedDate = formatter.format(value);
- jgen.writeString(formattedDate);
- }
- }
然后在你的POJO上找到日期的get方法
- @JsonSerialize(using = CustomDateSerializer.class)
- public Date getCreateAt() {
- return createAt;
- }
- @JsonSerialize(using = CustomDateSerializer.class)
- public Date getCreateAt() {
- return createAt;
- }
