Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map json to object with retrofit (GSON)

Json I get from Server is :

 {
"data" :   [
      { "id":1, "url": "http://example.com/image1"   },
      { "id":2, "url": "http://example.com/image2"   },
      { "id":3, "url": "http://example.com/image3"   }
  ]
}

and the class I have for mapping is

public class Repository {
    private List<Event> events;
}

although I get a success from retrofit I can't map it to my object. Also I have class Event with int id and String url.

like image 878
vicolored Avatar asked Sep 13 '15 07:09

vicolored


1 Answers

change it like this

public class Repository {
    private List<Event> data;
}

or

public class Repository {
    @SerializedName("data")
    private List<Event> events;
}
like image 81
Derek Fung Avatar answered Oct 12 '22 11:10

Derek Fung