Islom Umarov Islom Umarov - 3 months ago 12
JSON Question

Retrofit gives error Expected BEGIN_OBJECT but was BEGIN_ARRAY

I just started using Retrofit library today. I am having problems getting some json data.

JSON data I'm trying to get looks like this:

[
{"id":"1","genre_id":"27","name":"aaaaa"},
{"id":"2","genre_id":"21","name":"bbbb"}
]


Please, help!

Answer

Simply change it into List

public class Example {
@SerializedName("id")
@Expose
private String id;
@SerializedName("genre_id")
@Expose
private String genreId;
@SerializedName("name")
@Expose
private String name;
   public String getId() {
        return id;
   }

   public void setId(String id) {
      this.id = id;
   }


   public String getGenreId() {
       return genreId;
   }


   public void setGenreId(String genreId) {
       this.genreId = genreId;
   }


   public String getName() {
      return name;
   }


    public void setName(String name) {
      this.name = name;
   }

}

In your call back class use like List<Example>

Hope this will helpful for others also.