ps0604
ps0604

Reputation: 1081

Convert json String to List<List<String> with Jackson

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

Answers (1)

cassiomolin
cassiomolin

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

Related Questions