Jackson : map nested object

J.Mengelle :

Using jackson, i wonder if it's possible du map json to Java with nested Object that are not like the json structure.

Here an exemple of what i want to do.

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

Is it possible ?

Felk :

Use the JsonUnwrapped annotation:

@JsonUnwrapped
private final SomeObject someObject;

which unwrappes all of SomeObject's fields into the parent, resulting in the following when serializing:

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=477002&siteId=1