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,227
  • 23
  • 133
  • 330

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
  • 124,154
  • 35
  • 280
  • 359