我有一個Json數據被從服務器中拉出。這些數據包含幾個對象和數組。使GsonRequest接受空列表或空數組
第一個模型是如下:
{
"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作爲背景asynctask):
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?
我想你應該最好告訴你的服務器管理員或後端工程師不要發送這樣的數據。他們要麼根本不發送孩子,要麼發送爲空。 – tasomaniac