简体   繁体   中英

Retrofit convert array of objects to GSON Error

I am using Retrofit to make a HTTP request which returns an array of object and I am getting the following errors:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY

The response returned is expected to be like this:

[ {key1: "value1", key2: "value2"}, {key1: "value1", key2: "value2"}, ... ]

I have the following class, for serializing the data:

public class data {
  private List<element> dataList;

  public List<element> getElements() {
   return dataList;
  }

  public class element {
    @SerializedName("key1")
    private String key1;

    @SerializedName("key2")
    private String key2;

    // Getters and Setters
  }
}

Please let me know if you have any ideas. Thanks

The error was actually in my implementation of Retrofit Callback. My implementation was expecting an object when it should be expecting an array in this case. Thanks everyone for the help.

Before

//*****MyData*****//
public class MyData {
  private List<Data> dataList;

  public List<Data> getElements() {
   return dataList;
  }

  public class Data {
    @SerializedName("key1")
    private String key1;

    @SerializedName("key2")
    private String key2;

    // Getters and Setters
  }
}

//*****Callback Implementation*****//
public class MyDataCallback extends Callback {
   public MyDataCallback(MyDataCallbackListener<MyData> myDataCallbackListener) {
        super(myDataCallbackListener);
    }

    @Override
    public void success(MyData data, Response response) {
        if (myDataCallbackListener != null) {
            myDataCallbackListener.onCallbackComplete(true, response, MyDataCallback.CALLBACK_SUCCESS_MESSAGE, data);
        }
    }
}

After

//*****Data*****//
public class Data {
    @SerializedName("key1")
    private String key1;

    @SerializedName("key2")
    private String key2;

    // Getters and Setters
}

//*****Callback Implementation*****//
public class MyDataCallback extends Callback {
   public MyDataCallback(MyDataCallbackListener<List<Data>> myDataCallbackListener) {
        super(myDataCallbackListener);
    }

    @Override
    public void success(List<Data> data, Response response) {
        if (myDataCallbackListener != null) {
            myDataCallbackListener.onCallbackComplete(true, response, MyDataCallback.CALLBACK_SUCCESS_MESSAGE, data);
        }
    }
}

As Dave mentioned in his comment, it does seem strange that you have recursion in the class that I am assuming is your response object. (your class "data" has a list of "data" objects).

I would suggest something a little more strait forward such as this:

public class ResponseObject {
  private ArrayList<DataObject> mDataObjects;

  public ArrayList<DataObject> getDataObjects() {
    return mDataObjects;
  }

  private class DataObject {
    private String key1;
    private String key2;

    public String getKey1() {
      return key1;
    }

    public String getKey2() {
      return key2;
    }

  }

}

or since you are local maybe you can buy Jake a beer :) From his photo, I would check Rouge Ales, 21 Amendment or my favorite last time I was in SF - Magnolia

It's not valid JSON to begin with an array. You need to instead return something like this:

{
    dataList: [
        {
            key1: "value1",
            key2: "value2"
        },
        {
            key1: "value3",
            key2: "value4"
        }
    ]
}

Then you can use GSON to deserialize that into your data class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM