SpiralDev
SpiralDev

Reputation: 7331

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!

Upvotes: 1

Views: 214

Answers (2)

Amir
Amir

Reputation: 16607

Your Object should be List of Your models something like this:

Model {
     String id;
     String genre_id;
     String name;
}

Then you should parse object with List<Model> . If you use this pattern you method in ServiceHelper should be something like this:

Call<List<Model>> getListOf();

Upvotes: 1

AMAN SINGH
AMAN SINGH

Reputation: 3561

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.

Upvotes: 2

Related Questions