将json映射到改造对象(GSON)(Map json to object with retrofit (GSON))
我从服务器获得的Json是:
{ "data" : [ { "id":1, "url": "http://example.com/image1" }, { "id":2, "url": "http://example.com/image2" }, { "id":3, "url": "http://example.com/image3" } ] }和我为映射的类是
public class Repository { private List<Event> events; }虽然我从改造中获得成功,但我无法将其映射到我的对象。 此外,我有int id和String url类Event 。
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.
最满意答案
像这样改变它
public class Repository { private List<Event> data; }要么
public class Repository { @SerializedName("data") private List<Event> events; }change it like this
public class Repository { private List<Event> data; }or
public class Repository { @SerializedName("data") private List<Event> events; }更多推荐
发布评论