将对象数组转换为GSON错误

时间:2022-02-19 16:45:59

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

我正在使用Retrofit来发出返回对象数组的HTTP请求,我得到以下错误:

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

如果你有什么想法,请告诉我。谢谢

3 个解决方案

#1


10  

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.

错误实际上是在我实现的Retrofit Callback中。在本例中,我的实现期望对象应该是数组。谢谢大家的帮助。

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);
        }
    }
}

#2


0  

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).

正如Dave在他的评论中提到的,在我假设是响应对象的类中出现了递归,这看起来确实很奇怪。(您的类“data”有一个“data”对象列表)。

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

或者因为你是本地人,也许你可以给杰克买杯啤酒:)从他的照片上,我可以看看《胭脂虫》、《21修正案》或我上次在旧金山时最喜欢的《木兰》

#3


-3  

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

以数组开头是无效的JSON。你需要返回如下内容:

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

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

然后可以使用GSON将其反序列化到数据类中。

#1


10  

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.

错误实际上是在我实现的Retrofit Callback中。在本例中,我的实现期望对象应该是数组。谢谢大家的帮助。

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);
        }
    }
}

#2


0  

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).

正如Dave在他的评论中提到的,在我假设是响应对象的类中出现了递归,这看起来确实很奇怪。(您的类“data”有一个“data”对象列表)。

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

或者因为你是本地人,也许你可以给杰克买杯啤酒:)从他的照片上,我可以看看《胭脂虫》、《21修正案》或我上次在旧金山时最喜欢的《木兰》

#3


-3  

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

以数组开头是无效的JSON。你需要返回如下内容:

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

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

然后可以使用GSON将其反序列化到数据类中。