2017-10-05 220 views
3

使用jackson,我想知道是否有可能将json映射到Java,嵌套对象不像json结构。Jackson:映射嵌套对象

这里是我想要做的例子。

JSON:

{ 
    a = "someValue", 
    b = "someValue", 
    c = "someValue" 
} 

的Java:

public class AnObject { 
    @JsonProperty("a") 
    private String value; 

    //Nested object 
    private SomeObject; 
} 

public class SomeObject { 
    @JsonProperty("b") 
    private String value1; 

    @JsonProperry("c") 
    private String value2; 
} 

这可能吗?

+0

使用注释是不可能的,但是你可以编写你自己的反序列化器,在那里你可以像管理Map一样管理Json并自己填充pojo字段。 https://fasterxml.github.io/jackson-databind/javadoc/2.3.0/com/fasterxml/jackson/databind/JsonDeserializer.html –

+0

@DmytroDovzhenko Pinging你让你知道这确实是可能的,看到我的回答 – Felk

回答

4

使用JsonUnwrapped注释:

@JsonUnwrapped 
private final SomeObject someObject; 

这unwrappes所有SomeObject的场为父母,导致下面的序列化时:

{"a":"foo","b":"bar","c":"baz"} 
+0

我认为它只适用于Serializate,我想要反序列化... 但我会试一试 –

+0

它也适用于反序列化。我可以给你一个工作的例子,如果你无法使其工作 – Felk

+0

有效地工作,即使文档只提到序列化。 谢谢! –

0

首先,这是一个JSON对象。

这是一个object literal

其次,这不是一个有效的格式化对象文字。 正确的一个是这样的:

{ "a" : "someValue", "b": "someValue", "c": "someValue"} 

接下来,评论sayd,你必须定义自己的解串器。

主营:

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { 

    String json = "{\"a\" : \"someValue\",\"b\" : \"someValue\",\"c\" : \"someValue\"}"; 

    final ObjectMapper om = 
     new ObjectMapper();// 
    om.registerSubtypes(AnObject.class); 
    SimpleModule module = new SimpleModule(); 
    module.addDeserializer(AnObject.class, new CustomDeserializer2()); 
    om.registerModule(module); 

    AnObject ob = om.readValue(json, AnObject.class); 
    System.out.println(ob.getValue()); 
    System.out.println(ob.getObject().getValue1()); 
    System.out.println(ob.getObject().getValue2()); 
} 

解串器:

public class CustomDeserializer2 extends StdDeserializer<AnObject> { 

private static final long serialVersionUID = -3483096770025118080L; 

public CustomDeserializer2() { 
    this(null); 
} 

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

@Override 
public AnObject deserialize(JsonParser jp, DeserializationContext ctxt) 
     throws IOException, JsonProcessingException { 
    JsonNode interNode = jp.getCodec().readTree(jp); 

    AnObject ob = new AnObject(); 

    if (interNode.get("a") != null) { 
     ob.setValue(interNode.get("a").toString()); 
    } 

    SomeObject obj = new SomeObject(); 

    if (interNode.get("b") != null) { 
     obj.setValue1(interNode.get("b").toString()); 
    } 
    if (interNode.get("c") != null) { 
     obj.setValue2(interNode.get("c").toString()); 
    } 

    ob.setObject(obj); 
    return ob; 
} 

型号:注重@JsonProperty某个字段

public class AnObject { 

@JsonProperty("a") 
private String value; 

private SomeObject object; 

public String getValue() { 
    return value; 
} 

public void setValue(String value) { 
    this.value = value; 
} 

public SomeObject getObject() { 
    return object; 
} 

public void setObject(SomeObject arg) { 
    object = arg; 
} 



public class SomeObject { 

private String value1; 
private String value2; 
public String getValue1() { 
    return value1; 
} 
public void setValue1(String value1) { 
    this.value1 = value1; 
} 
public String getValue2() { 
    return value2; 
} 
public void setValue2(String value2) { 
    this.value2 = value2; 
} 

再见

1

使用ObjectMapper可以转换成JSON字符串对象。 通过someObject字段在您的AnObject类中使用JsonUnwrapped

@JsonUnwrapped 
private SomeObject someObject; 

然后读取JSON字符串并将其转换为AnObject。

ObjectMapper mapper = new ObjectMapper(); 
try { 
    AnObject anObject1 = mapper.readValue(jsonString, AnObject.class); 
} catch (IOException e) { 
    e.printStackTrace(); 
}