![](/img/trans.png)
[英]Unable to deserialize json array to class object using jackson fasterxml in scala
[英]Unable to deserialize Object[] to class using Jackson
我試圖將我的代碼Json數組中的這個Json
元素反序列化為自定義類..但是我不能:
ObjectMapper OMapper = new ObjectMapper();
OMapper.configure(Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
OMapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
OMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
OMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
OMapper.enable(SerializationFeature.INDENT_OUTPUT);
JsonNode node = OMapper.readTree("{\"headers\":[\"st1\", \"ddf2\", \"sdsd\"]}");
headers h = OMapper.treeToValue(node.get("headers"), headers.class);
這是課程:
public class headers{
public headers(){
System.out.println("dssdsd");
}
@JsonSetter
public void set(){
System.out.println("ASAASAAS");
}
@JsonGetter()
public JsonNode get(int index){
System.out.println("XXXXXXXXXx");
return null;
}
}
但我收到此錯誤:
com.fasterxml.jackson.databind.exc.MismatchedInputException:無法在[來源:UNKNOWN; 行:-1,列:-1]位於com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:62)位於com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1307)在com.fasterxml.jackson.Databind.deser.com上的com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1070)處的com.fasterxml.jackson.databind.DeserializationContext.java:1116處。 com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOtherOther(BeanDeserializer.java:173)處的.deserializeFromArray(BeanDeserializerBase.java:1447)com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(Bean:149。 )
您應該將headers
指定為數組。
headers[] h = OMapper.treeToValue(node.get("headers"), headers[].class);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.