杰克逊:映射嵌套对象

时间:2017-10-05 13:04:00

标签: java jackson2

使用jackson,我想知道是否可以使用嵌套的Object将json映射到Java,而不是像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;
}

有可能吗?

3 个答案:

答案 0 :(得分:5)

使用JsonUnwrapped注释:

@JsonUnwrapped
private final SomeObject someObject;

将所有SomeObject的字段解压缩到父级中,在序列化时产生以下结果:

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

答案 1 :(得分:2)

使用ObjectMapper可以将JSON字符串转换为Object。 在someObject类中使用JsonUnwrapped而不是someObject字段。

@JsonUnwrapped
private SomeObject someObject;

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

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

答案 2 :(得分:0)

首先,这是一个JSON对象。

它是object literal

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

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

接下来,正如评论中所说,你必须定义自己的反序列化器。

主:

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;
}

模型:注意A字段上的@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;
}

再见