Java 使用Jackson fasterxml反序列化属性类

Java 使用Jackson fasterxml反序列化属性类,java,json,jackson,fasterxml,mongo-jackson-mapper,Java,Json,Jackson,Fasterxml,Mongo Jackson Mapper,我试图使用Jackson和fasterxml(vs codehaus)来反序列化存储在mongoDb中的一些数据。查询返回的是正确的json字符串,但在我们尝试将其转换为对象时,我一直遇到此错误: Can not deserialize instance of java.lang.String out of START_OBJECT token Properties("property1") 类别: package com.sharecare.service.segment; imp

我试图使用Jackson和fasterxml(vs codehaus)来反序列化存储在mongoDb中的一些数据。查询返回的是正确的json字符串,但在我们尝试将其转换为对象时,我一直遇到此错误:

Can not deserialize instance of java.lang.String out of START_OBJECT token Properties("property1")
类别:

    package com.sharecare.service.segment;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.jamconsultg.jamplatform.domain.segmentation.Segment;

import java.util.List;
import java.util.Properties;

/**
 * Created by wawada on 1/15/16.
 */

public class SubSkyScraper{

    @JsonProperty("_id")
    private String id;

    private String name;

    private Properties properties;

    @JsonProperty("_segments")
    private List<Segment> segments;

    public SubSkyScraper() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public List<Segment> getSegments() {
        return segments;
    }

    public void setSegments(List<Segment> segments) {
        this.segments = segments;
    }
}
输出:

    com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
 at [Source: {
"_id": "objectid" ,
"_segments" : [ "global"] , 
"name" : "sc" , 
"properties" : { 
    "property1": {
        "property2" : "value1"
    },
    "property3" : { "property4": "value2"} , 
    "property5": {
        "property6" : { 
                    "property7": "value3" , 
                    "property8": "value4"
    }
}
}; line: 6, column: 5] (through reference chain: com.sharecare.service.segment.SubSkyScraper["properties"]->java.util.Properties["property1"])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:857)
    at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:62)
    at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringMap(MapDeserializer.java:495)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:341)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:26)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:258)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2726)
    at com.sharecare.service.segment.ObjectMapperTest.testGettingTheObject(ObjectMapperTest.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
mongodb中保存的json结构如下:

{
"_id": "objectid" ,
"_segments" : [ "global "] , 
"name" : "sc" , 
"properties" : { 
    "property1": {
        "property2" : "value1"
    },
    "property3" : { "property4": "value2"} , 
    "property5": {
        "property6" : { 
                    "property7": "value3" , 
                    "property8": "value4"
    }
}
}
如何让Jackson正确地反序列化属性

PS:这在codehaus中完美地工作了

以下是我的方法:

我正在使用自定义反序列化程序,如下所示:

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

public class PropertiesDeserializer extends StdDeserializer<Properties> {
    private static final long serialVersionUID = 2743004642083542567L;

    public PropertiesDeserializer() { 
        this(null); 
    } 

    public PropertiesDeserializer(Class<?> vc) { 
        super(vc); 
    }

    @Override
    public Properties deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        JsonDeserializer<Object> mapDeserializer = findDeserializer(ctxt, ctxt.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class), null);
        @SuppressWarnings("unchecked")
        Map<String,Object> rawValue = (Map<String, Object>) mapDeserializer.deserialize(jp, ctxt);
        if (rawValue == null) {
            return null;
        }
        Properties mappedValue = new Properties();
        rawValue.forEach((key, value) -> mappedValue.put(key, value));
        return mappedValue;
    }

}

如果要解析嵌套的java.util.Properties,可以在遍历rawValue map时使用反序列化方法进行解析

这个问题类似于希望有帮助。我们已经从摩天大楼集合中找到了摩天大楼类。我们有各种变量设置。我看不出诚实有什么相似之处。你能详细说明一下吗?当我检查你的json时,我发现这是类似的。基本上是尝试使用Jackson反序列化json中的json。这是不可能的,正如我之前粘贴的链接中所述。他详细地提到了一种方法。我可能认为你需要提到@JsonProperty(“\u properties”)对于属性。我还没有尝试过POC,但我坚信可能是这样。在mongodb中,properties变量的名称是properties而不是properties。我们使用@JsonProperty注释的是_段,请确保您在Jackson 2.x中使用的注释是为Jackson 2.x定义的注释(也就是说,映射器和注释包都来自
com.fasterxml.jackson
)。升级的第一个问题是jackson 1.x注释仍然被意外使用。
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

public class PropertiesDeserializer extends StdDeserializer<Properties> {
    private static final long serialVersionUID = 2743004642083542567L;

    public PropertiesDeserializer() { 
        this(null); 
    } 

    public PropertiesDeserializer(Class<?> vc) { 
        super(vc); 
    }

    @Override
    public Properties deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        JsonDeserializer<Object> mapDeserializer = findDeserializer(ctxt, ctxt.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class), null);
        @SuppressWarnings("unchecked")
        Map<String,Object> rawValue = (Map<String, Object>) mapDeserializer.deserialize(jp, ctxt);
        if (rawValue == null) {
            return null;
        }
        Properties mappedValue = new Properties();
        rawValue.forEach((key, value) -> mappedValue.put(key, value));
        return mappedValue;
    }

}
ObjectMapper mapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addDeserializer(Properties.class, new PropertiesDeserializer());
mapper.registerModule(simpleModule);