0

I have Json response as

"Test1": {
        "Test2": [
            [
                0,
                "US TV",
                []
            ]
        ]
}

I have created a Test2 class like

    @JsonProperty("Test2")
    @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
    Test2[] arrayObjects; 

And not sure how to give [] in Test2 class, can anyone suggest how to create Pojo for the mentioned Json response in Java?

0

1 Answer 1

2

Declare the field like this:

@JsonProperty("Test2")
Object[][] test2;

or like this:

@JsonProperty("Test2")
List<List<Object>> test2;

The type is Object since the inner array contains a mix of number, string, and array.

The name of the field doesn't matter, since you have @JsonProperty naming it for the JSON.

You can of course also do List<Object[]> or List<Object>[], but those seem a bit weird.


UPDATE: Proof that above code works:

Foo.java

@JsonRootName("Test1")
class Foo {
    @JsonProperty("Test2")
    List<List<Object>> bar;

    public static void main(String[] args) throws Exception {
        Foo foo = new ObjectMapper()
                .configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true)
                .readValue(new File("test.json"), Foo.class);
        System.out.println(foo.bar);
    }
}

test.json

{
    "Test1": {
        "Test2": [
            [
                0,
                "US TV",
                []
            ]
        ]
    }
}

Output

[[0, US TV, []]]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.