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



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?

你还没有展示你的responsePOJO类是什么样子的,但是是的,你可以。 您可以做的是像往常一样创建所有已知字段,然后将未知字段存储为地图并利用杰克逊JsonAnySetter注释。

这方面的一个例子是将其他字段存储在地图中,如下所示:

private Map<String, Object> otherFields = new HashMap<>();
@JsonAnySetter
public void set(String name, Object value) {
    otherFields.put(name, value)
}

然后,Jackson 将为标准字段列表中找不到的任何字段调用此set方法。

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

abstract class PojoParent {
    int qty1;
    int qty2;
    boolean override;
    ...
}
class Pojo1 extends PojoParent {
    String a;
    ...
}
class Pojo2 extends PojoParent {
    String e;
    ...
}

可能有一些技巧可以只用一个类来做到这一点,但我建议使用最简单和最全面的解决方案。

你可以像这样使用 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 字符串。

最新更新