Deserialize json array where attribute value is array java

Savas P :

I know this is very basic, but I have a problem with deserializing json using jackson when the format is like this :

I created a class Person with id, name and place and tried to read the results from API call using jackson (using the @JsonProperty annotation), but when I debug the persons variable is null:

 json body:
  { people:[  
   {  
    "id":"0",
    "name":"Bob",
    "place":"Colorado",
   },
   {  
    "id":"1",
    "name":"John",
    "place":"Chicago",
   },
   {  
    "id":"2",
    "name":"Marry",
    "place":"Miami",
   }
 ]}


 RequestEntity<Void> reqEntity = RequestEntity.get(new URI(url))
                .accept(MediaType.APPLICATION_JSON)
                .build();

 ResponseEntity<List<Person>> persons = template.exchange(reqEntity, new ParameterizedTypeReference<List<Person>>() {});
Samuel Philipp :

You should wrap your List<Person> in another Response object, which has a people field, containing your list:

public class PeopleResponse {
    private List<Person> people;

    // getter and setter
}

Then you can change your ResponseEntity according to that:

ResponseEntity<PeopleResponse> response = template.exchange(reqEntity, new ParameterizedTypeReference<PeopleResponse>() {});
List<Person> people = response.getBody().getPeople();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=148544&siteId=1