当json有其他字段时,使用jackson objectmapper进行反序列化失败。如何解决?

时间:2014-04-24 00:02:15

标签: json jackson deserialization

提前致谢。我使用的是版本1.9.2

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.2</version>
    </dependency>


org.codehaus.jackson.map.ObjectMapper jsonMapper = new org.codehaus.jackson.map.
jsonMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES , false);
JsonNode listNode = jsonMapper.readTree(json);
Iterator<JsonNode> it = listNode.getElements();
        while (it.hasNext()) {
            JsonNode moNode = it.next();
            String className = moNode.get("objectClass").asText();
            String moNodeText = moNode.toString();               
            Class<?> moClass = Class.forName(className);                      
            Object ob = jsonMapper.readValue(moNodeText, moClass);                
            MyObject mo = (MyObject) ob;
            moSet.add(mo);
        }

但它一直未能指向json中的其他字段。

(was java.lang.UnsupportedOperationException) (through reference chain: com.xxx.MyObject["additional_field_Json"])

我尝试过解析器但得到了同样的错误。有什么帮助吗?

感谢

戈皮

2 >>>> nodeText :{"objectclass":"com.xxx.xxx.MetaInfo$Flow",............,"fieldonlyexistinjson":["VALUE1","VALUE2","VALUE3"],"subFlows":[]}

    2014-04-23 17:39:48,549 - ERROR main com.xxx.xxx.TestMetaDataCache.testImportMetadataFromJson (TestMetaDataCache.java:86) exception 
org.codehaus.jackson.map.JsonMappingException: (was java.lang.UnsupportedOperationException) (through reference chain: com.xxx.xxx.Flow["objectTypes"])
    at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:218)
    at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:183)
    at org.codehaus.jackson.map.deser.BeanDeserializer.wrapAndThrow(BeanDeserializer.java:1462)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:699)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
    at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2695)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1279)
    at org.codehaus.jackson.JsonParser.readValueAs(JsonParser.java:1336)
    at com.xxx.xxx.TestMetaDataCache.testImportMetadataFromJson(TestMetaDataCache.java:80)

1 个答案:

答案 0 :(得分:0)

不,这不是同一个异常:“UnsupportedOperationException”不是你得到的无法识别的属性。你可以包括异常的完整堆栈跟踪吗?

我的猜测是你在某处有一个不可变的List或Map,而deserializer正在尝试向它添加条目。