无法使用Jackson

时间:2017-04-10 16:04:11

标签: java json jackson deserialization

我试图将我的代码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:不能   [源代码:在源代码中反序列化START_ARRAY标记中的标题实例:   未知; line:-1,column:-1] at   com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:62)     在   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)     在   com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1447)     在   com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:173)     在   com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:149)

1 个答案:

答案 0 :(得分:1)

您应该将headers指定为数组。

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