Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

This is my arraylist

 ArrayList<Eat> eatList = gson.fromJson(jsonString, new 
 TypeToken<ArrayList<Eat>>() {
            }.getType());

This is my json: http://www.mocky.io/v2/592fdc32110000ef12b392cc

and this is my model

public class Eat{

private String title,firstItemTitle,firstItemSutitle,
secondItemTitle,secondItemSutitle,
firstItemPrice,secondItemPrice,
firstItemImage,secondItemImage;


public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getFirstItemTitle() {
    return firstItemTitle;
}

public void setFirstItemTitle(String firstItemTitle) {
    this.firstItemTitle = firstItemTitle;
}

public String getFirstItemSutitle() {
    return firstItemSutitle;
}

 public void setFirstItemSutitle(String firstItemSutitle) {
    this.firstItemSutitle = firstItemSutitle;
 }

 }
like image 800
Franko Loshe Avatar asked Dec 01 '25 12:12

Franko Loshe


1 Answers

Since your JSON is not a JSON array, but rather a JSON object containing an array, you would need to write a class that contains the ArrayList:

public class EatResponse {
    @SerializedName("eat")
    private ArrayList<Eat> eatList;

    public ArrayList<Eat> getEatList() {
        return eatList;
    }
}

Then, you just need to parse that from your JSON with a call that would look something like this:

EatResponse response = gson.fromJson(json, EatResponse.class);
ArrayList<Eat> eatList = response.getEatList();
like image 156
npace Avatar answered Dec 03 '25 00:12

npace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!