在杰克逊将JSON反序列化为类时,找不到类型[simple type,class java.lang.Class]类型的(Map)密钥反序列化器

时间:2017-01-02 12:25:25

标签: json jackson json-deserialization

解析此JSON对象:

{"topology": [
{"id": "4cdbc040-657a-4847-b266-7e31d9e2c3d9","name": "PhysicalTOPO","layer": "datalink","nodes": null,"links": null,"sub-layer": "ethernet"},
{"id": "4acbd130-846b-3536-a142-8a42d8a3c4b8","name": "LogicalTOPO","layer": "ip","nodes": null,"links": null,"sub-layer": "ethernet"},
{"id": "9acbd139-946b-9536-a149-9a42d9a3c4b9","name": "NetworkTOPO","layer": "ip","nodes": null,"links": null,"sub-layer": "ethernet"}
]}

我已经创建了VO类和解析函数,如下所示:

public class TopologyVo extends Vo {
    @JsonProperty("topology")
    public Topology[] topology;
    //public List<Topology> topology; -- both have same exception

    public static class Topology extends Vo{
        @JsonProperty("id")
        public String id;
        @JsonProperty("name")
        public String name;
        @JsonProperty("layer")
        public String layer;
        @JsonProperty("nodes")
        public Object nodes; // it does not matter to map null to Object now
        @JsonProperty("links")
        public Object links;
        @JsonProperty("sub-layer")
        public String subLayer;
    }
}

// caller
...
TopologyVo vo = (TopologyVo)parse(content, TopologyVo.class);
...

public Object parse(String input, Class<?> mapClass) throws Exception {
    Object object = null;
    ObjectMapper mapper = new ObjectMapper();

    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    object = mapper.readValue(input, mapClass);
    return object;
}

导致例外:

com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class java.lang.Class]
at com.fasterxml.jackson.databind.deser.DeserializerCache._handleUnknownKeyDeserializer(DeserializerCache.java:580)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findKeyDeserializer(DeserializerCache.java:170)
at com.fasterxml.jackson.databind.DeserializationContext.findKeyDeserializer(DeserializationContext.java:404)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.createContextual(MapDeserializer.java:232)
at com.fasterxml.jackson.databind.DeserializationContext.handleSecondaryContextualization(DeserializationContext.java:572)
at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:369)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:697)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:438)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:294)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:243)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:144)
at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:381)
at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:3095)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2989)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2098)
at test.ApiTest.parse(ApiTest.java:195)

由于我的VO课程中没有Map,我不知道如何实现&#34;密钥解串器&#34;在回答类似问题(Can not find a (Map) Key deserializer for type [simple type, class com.comcast.ivr.core.domain.AutoHandlingSlotKey])。

这个问题是来自VO中子类的数组(或列表/集合)吗?

1 个答案:

答案 0 :(得分:1)

这对我很有用:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {

    public static void main(String[] args) throws Exception {
        String jsonStr = "{\"status\": \"OK\",\"origin_addresses\": [ \"Vancouver, BC, Canada\", \"Seattle, État de Washington, États-Unis\" ],\"destination_addresses\": [ \"San Francisco, Californie, États-Unis\", \"Victoria, BC, Canada\" ],\"rows\": [ {\"elements\": [ {\"status\": \"OK\",\"duration\": {\"value\": 340110,\"text\": \"3 jours 22 heures\"},\"distance\": {\"value\": 1734542,\"text\": \"1 735 km\"}}, {\"status\": \"OK\",\"duration\": {\"value\": 24487,\"text\": \"6 heures 48 minutes\"},\"distance\": {\"value\": 129324,\"text\": \"129 km\"}} ]}, {\"elements\": [ {\"status\": \"OK\",\"duration\": {\"value\": 288834,\"text\": \"3 jours 8 heures\"},\"distance\": {\"value\": 1489604,\"text\": \"1 490 km\"}}, {\"status\": \"OK\",\"duration\": {\"value\": 14388,\"text\": \"4 heures 0 minutes\"},\"distance\": {\"value\": 135822,\"text\": \"136 km\"}} ]} ]}";

        TopologyVo vo = (TopologyVo) parse(jsonStr, TopologyVo.class);
        System.out.println(vo);
    }

    public static Object parse(String input, Class<?> mapClass)
            throws Exception {
        Object object = null;
        ObjectMapper mapper = new ObjectMapper();

        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
                false);
        object = mapper.readValue(input, mapClass);
        return object;
    }
}

class Vo {

}

class TopologyVo extends Vo {
    @JsonProperty("topology")
    public Topology[] topology;

    static class Topology extends Vo {
        @JsonProperty("id")
        public String id;
        @JsonProperty("name")
        public String name;
        @JsonProperty("layer")
        public String layer;
        @JsonProperty("nodes")
        public Object nodes; // it does not matter to map null to Object now
        @JsonProperty("links")
        public Object links;
        @JsonProperty("sub-layer")
        public String subLayer;
    }

}