2017-04-10 136 views
0

我試圖反序列化這個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;]處將START_ARRAY標記的實例反序列化line:-1,column:-1] at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:62) at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java :1307) 在 com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1116) 在 com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1070) 在 融爲一體。 fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1447) at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializ eOther(BeanDeserializer.java:173) 在 com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:149)

回答

1

應指定headers作爲數組。

headers[] h = OMapper.treeToValue(node.get("headers"), headers[].class); 
+0

謝謝,但如何從標題[]讀取JsonNode?吸氣劑和吸附劑? – Jason4Ever