使用Jackson的两个不同JSON的一个POJO

时间:2016-06-03 16:59:06

标签: java json jackson deserialization

JSON 1:

[
    {
        "a": 23118,
        "b": "3373141",
        "c": "abcd",
        "d": "d_name",
        "override": false,
        "qty1": 2000.0,
        "qty2": 2000.0,
        "qty3": 2000.0,
        "qty4": 2000.0,
        "update": "01:00:00"
    },
    {},
    {},
    ...
]

JSON 2:

[
    {
        "e": 2317418,
        "f": "XYZ",
        "g": "abcdef",
        "h": "h_name",
        "override": false,
        "qty1": 2000.0,
        "qty2": 2000.0,
        "qty3": 2000.0,
        "qty4": 2000.0
    },
    {},
    {},
    ...
]

代码:

ObjectMapper objectMapper = new ObjectMapper();
responsePOJO responsePOJOObj = objectMapper.readValue(JSONString, responsePOJO.class);

是否可以使用一个POJO类转换JSON 1和JSON 2?或者我必须创建两个不同的POJO?

3 个答案:

答案 0 :(得分:0)

您尚未显示responsePOJO课程的样子,但您可以。您可以做的是像往常一样创建所有已知字段,然后将未知数存储为地图并使用Jacksons JsonAnySetter注释。

这样的例子就像将其他字段存储在地图中一样,如下所示:

private Map<String, Object> otherFields = new HashMap<>();

@JsonAnySetter
public void set(String name, Object value) {
    otherFields.put(name, value)
}

杰克逊将为您在标准字段列表中找不到的任何字段调用此set方法

答案 1 :(得分:0)

如果只是某些字段不同,我建议使用2个POJO和一个包含公共字段的公共父类。

abstract class PojoParent {
    int qty1;
    int qty2;
    boolean override;
    ...
}

class Pojo1 extends PojoParent {
    String a;
    ...
}

class Pojo2 extends PojoParent {
    String e;
    ...
}

只有一个班级可能会有一些技巧,但我建议你选择最简单,最全面的解决方案。

答案 2 :(得分:0)

您可以使用JsonIgnoreProperties这样的注释:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;

public class PojoDeserialize {

    public static class ResponsePOJO {
        @JsonIgnoreProperties(ignoreUnknown = true)
        public String a;
        @JsonIgnoreProperties(ignoreUnknown = true)
        public String b;
        @JsonIgnoreProperties(ignoreUnknown = true)
        public String c;
        @JsonIgnoreProperties(ignoreUnknown = true)
        public String d;
        @JsonIgnoreProperties(ignoreUnknown = true)
        public String e;
        @JsonIgnoreProperties(ignoreUnknown = true)
        public String f;
        @JsonIgnoreProperties(ignoreUnknown = true)
        public String g;
        @JsonIgnoreProperties(ignoreUnknown = true)
        public String h;
    }

    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        String json1 = "{\"a\": 23118,\"b\": \"3373141\",\"c\": \"abcd\",\"d\": \"d_name\"}";
        ResponsePOJO resp1 = objectMapper.readValue(json1, ResponsePOJO.class);
        assert resp1.a != null;
        assert resp1.b != null;
        assert resp1.c != null;
        assert resp1.d != null;
        assert resp1.e == null;
        assert resp1.f == null;
        assert resp1.g == null;
        assert resp1.h == null;
        String json2 = "{\"e\": 2317418,\"f\": \"XYZ\",\"g\": \"abcdef\",\"h\": \"h_name\"}";
        ResponsePOJO resp2 = objectMapper.readValue(json2, ResponsePOJO.class);
        assert resp2.a == null;
        assert resp2.b == null;
        assert resp2.c == null;
        assert resp2.d == null;
        assert resp2.e != null;
        assert resp2.f != null;
        assert resp2.g != null;
        assert resp2.h != null;
    }

}

所以这样你可以保留一个类但转换两个不同的JSON字符串。