6

使用杰克逊,我想知道是否可以将 json 映射到 Java,其中嵌套的对象与 json 结构不同。

这是我想做的一个例子。

杰森:

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

爪哇:

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

可能吗 ?

4

3 回答 3

7

使用JsonUnwrapped注释:

@JsonUnwrapped
private final SomeObject someObject;

它将所有SomeObject's 字段解包到父级中,在序列化时导致以下结果:

{"a":"foo","b":"bar","c":"baz"}
于 2017-10-05T15:10:52.750 回答
1

使用 ObjectMapper,您可以将 JSON 字符串转换为 Object。在您的 AnObject 类中对 someObject 字段使用JsonUnwrapped 。

@JsonUnwrapped
private SomeObject someObject;

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

ObjectMapper mapper = new ObjectMapper();
try {
   AnObject anObject1 = mapper.readValue(jsonString, AnObject.class);   
} catch (IOException e) {
    e.printStackTrace();
}
于 2017-10-05T15:49:31.903 回答
0

首先,这是一个 JSON 对象。

这是一个对象字面量

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

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

再见

于 2017-10-05T14:49:25.707 回答