简体   繁体   English

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

[英]One POJO for two different JSONs using Jackson

JSON 1: 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: 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
    },
    {},
    {},
    ...
]

Code: 码:

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

Is it possible to convert JSON 1 and JSON 2 using one POJO class? 是否可以使用一个POJO类转换JSON 1和JSON 2? Or I have to create two different POJOs? 或者我必须创建两个不同的POJO?

You haven't shown what your responsePOJO class looks like but yes you can. 你还没有展示你的responsePOJO课程的样子,但是你可以。 What you can do is create all of your known fields as usual and then have the unknowns stored as a map and make use of Jacksons JsonAnySetter annotation. 您可以做的是像往常一样创建所有已知字段,然后将未知数存储为地图并使用Jacksons JsonAnySetter注释。

An example of this would be something like storing the other fields in a map, like this: 例如,将其他字段存储在地图中,如下所示:

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

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

Jackson will then call this set method for any field it can't find in your standard field list 然后杰克逊将为您在标准字段列表中找不到的任何字段调用此set方法

If just some fields differ, I would suggest using 2 POJO with a common parent class containing the common fields. 如果只是某些字段不同,我建议使用2个POJO和一个包含公共字段的公共父类。

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

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

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

There's probably some tricks to do it with only one class but I'd advise going with the most simple and comprehensive solution. 只有一个类可能有一些技巧,但我建议采用最简单和全面的解决方案。

You can use the JsonIgnoreProperties annotation like this: 您可以像这样使用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;
    }

}

So this way you can keep one class but convert two different JSON strings. 所以这样你可以保留一个类但转换两个不同的JSON字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM