【发布时间】:2014-03-03 09:19:47
【问题描述】:
我有一个要从服务器中提取的 Json 数据。该数据包含多个对象和数组。
第一个模型如下:
{
"results": [
{
"id": "17",
"name": "Accessories",
"child": [
{
"id": "371",
"name": "Belt"
},
{
"id": "55",
"name": "Derp"
},
...
]
}
]
}
但是,某些results 数组没有child 数组。相反,它有一个空值的字符串。
{
"results": [
{
"id": "19",
"name": "Stuff",
"child": ""
}
]
}
当代码执行时,它返回这一行:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING
这就是模型的样子:
public class CategoryModel {
@SerializedName("id")
private String category_id;
private String name;
private ArrayList<CategoryChildModel> child;
...
}
这就是我实现 GsonRequest 的方式(使用 Volley 作为后台异步任务):
private void loadCategory() {
mRequestQueue = Volley.newRequestQueue(getActivity());
String url = Constants.CATEGORIES_LIST;
GsonRequest<CategoryContainer> myReq = new GsonRequest<CategoryContainer>(
Request.Method.GET, url, CategoryContainer.class,
createMyReqSuccessListener(), createMyReqErrorListener());
mRequestQueue.add(myReq);
}
那么,有人知道如何让空对象通过 GsonRequest 吗?
【问题讨论】:
-
我认为你最好告诉你的服务器管理员或后端工程师不要这样发送数据。他们要么根本不发送孩子,要么将其作为 null 发送。