2

I call a WS that returns a Json object like follows:

 { "id": "salton", "name": "salton", } 

which I parse without any problem using

ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(jsonStr, Show.class); 

Then I have another WS that return a list of objects, as follows

{ "id": "saltonId", "name": "salton", }, { "id": "elCordeLaCiutat", "name": "elCordeLaCiutat", } 

which I want to parse using

ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(jsonStr, List<Show.class>.class); 

but I got compilation problems

Multiple markers at this line - List cannot be resolved to a variable - Syntax error on token ">", byte expected after this token 
8
  • did you include java.util.List? Commented Jan 22, 2018 at 8:59
  • yes, its included in the import Commented Jan 22, 2018 at 9:00
  • why is .class inside a generic? Commented Jan 22, 2018 at 9:00
  • 1
    Use mapper.readValue(jsonStr, mapper.getTypeFactory().constructCollectionType(List.class, Show.class)); Commented Jan 22, 2018 at 9:01
  • Your list of objects should be wrapped by brackets. Commented Jan 22, 2018 at 9:08

2 Answers 2

1

A list of objects should be wrapped in [] as follows

[ { "id": "saltonId", "name": "salton", }, { "id": "elCordeLaCiutat", "name": "elCordeLaCiutat", } ] 

which you can unmarchal like that:

ObjectMapper mapper = new ObjectMapper(); List<Show> shows = Arrays.asList(mapper.readValue(json, Show[].class)); 
Sign up to request clarification or add additional context in comments.

Comments

0
Type listType = new TypeToken<List<Show>>() {}.getType(); return mapper.readValue(jsonStr, listType.class); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.