Search code examples
javajsonjacksonobjectmapper

Deserializing Array of arrays from json


I am trying to deserialize an array of array of Strings using ObjectMapper. The input has the following structure: [["key", "value"], ["Car", "1"], ["SUV", "1.1"]]

I have tried to de-serialize this by using the following method:

JavaType itemType = objectMapper.getTypeFactory().constructCollectionType(List.class, Array.class);
List<T> mutableList = objectMapper.readValue(json.or("[]"), itemType);

However, the compiler complains of raw use of parameterized class. Could someone please explain how to serialize such an input ?


Solution

  • The JSON you have is an Array with Array of string object and error message indicates to specify the exact type, so I would suggest to use TypeReference with exact type

    List<List<String>> listCar = objectMapper.readValue(json.or("[]"), new TypeReference<List<List<String>>>(){});