简体   繁体   中英

One POJO for two different JSONs using Jackson

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

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? Or I have to create two different POJOs?

You haven't shown what your responsePOJO class looks like but yes you can. 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.

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

If just some fields differ, I would suggest using 2 POJO with a common parent class containing the common fields.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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