1

I have a json String that's a list of a list of strings, and need to convert it to objects of class List<List<String>>.

Here it says how to convert from string to a list, but it doesn't work for me as I cannot specify the List<List<String>> class in constructCollectionType

This is my attempt, that doesn't work:

ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = objectMapper.getTypeFactory();
List<List<String>> list = objectMapper.readValue(response,
   typeFactory.constructCollectionType(List.class, List.class));

How to achieve this?

ps0604
  • 1,513
  • 20
  • 113
  • 274

1 Answers1

1

Use TypeReference<T>:

TypeReference<List<List<String>>> typeReference = 
        new TypeReference<List<List<String>>>() {};
List<List<String>> list = mapper.readValue(json, typeReference);
cassiomolin
  • 113,708
  • 29
  • 249
  • 319