Osman Corluk
Osman Corluk

Reputation: 365

Serialization Json mixed structure with jackson

Please check the sample json data:

String INPUT = "{\"a\":[1,2,{\"b\":true},3],\"c\":3}";

I would like to parse that json with jackson ObjectMapper. As you can see "a" is an array holds both integer and object. How can I define that a variable in POJO using anotations? Thanks in advance

Upvotes: 0

Views: 92

Answers (1)

Derrick
Derrick

Reputation: 4437

You can create pojo like below, since the array holds different objects, pojo will have array list of type Object to accept every type -

public class JsonInput {

            @JsonProperty("a")
            private List<Object> a = new ArrayList<Object>();
            @JsonProperty("c")
            private Integer c;

            public List<Object> getA() {
                return a;
            }

            public void setA(List<Object> a) {
                this.a = a;
            }

            public Integer getC() {
                return c;
            }

            public void setC(Integer c) {
                this.c = c;
            }
        }

Note: You may not have further control over the objects inside the array of json input to map it to the pojo class until it holds any specific type.

Upvotes: 1

Related Questions