Reputation: 1081
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?
Upvotes: 1
Views: 940
Reputation: 131127
Use TypeReference<T>
:
TypeReference<List<List<String>>> typeReference =
new TypeReference<List<List<String>>>() {};
List<List<String>> list = mapper.readValue(json, typeReference);
Upvotes: 1