问题
I have a Json data to be pulled from a server. This data contains several objects and arrays.
The first model is as follows:
{
"results": [
{
"id": "17",
"name": "Accessories",
"child": [
{
"id": "371",
"name": "Belt"
},
{
"id": "55",
"name": "Derp"
},
...
]
}
]
}
However, some of the results array doesn't have child array. Instead, it have a String with an empty value.
{
"results": [
{
"id": "19",
"name": "Stuff",
"child": ""
}
]
}
When the code is executed, it returns this line:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING
This is how the Model looks like:
public class CategoryModel {
@SerializedName("id")
private String category_id;
private String name;
private ArrayList<CategoryChildModel> child;
...
}
And this is how I implement the GsonRequest (which using Volley as background 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);
}
So, anyone knows how to make null object pass through GsonRequest?
回答1:
Actually your json response should return an empty array not a string for null cases. But if you don't have an option to change server's response then you may try to write a custom json deserializer:
class ChildDeserializer implements JsonDeserializer<ChildHolder> {
@Override
public ChildHolder deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String currentValueOfChild = json.toString();
Log.d("ChildDeserializer", "ChildDeserializer: child=" + currentValueOfChild);
ChildHolder childHolder = null;
if (json instanceof JsonArray) {
Log.d("ChildDeserializer", "ChildDeserializer: We have an array for 'child'");
Type listType = new TypeToken<List<Child>>() {}.getType();
JsonArray jsonArray= json.getAsJsonArray();
childHolder = new ChildHolder();
childHolder.childList = context.deserialize(jsonArray, listType);
}
return childHolder;
}
}
Your response java model should look like below:
class Response {
List<Result> results;
}
class Result {
private String id, name;
private ChildHolder child;
}
class ChildHolder {
private List<Child> childList;
}
class Child {
private String id, name;
}
Apply deserializer while parsing json to java model:
String jsonTest1 = "{\"results\":[{\"id\":\"17\",\"name\":\"Accessories\",\"child\":[{\"id\":\"371\",\"name\":\"Belt\"},{\"id\":\"55\",\"name\":\"Derp\"}]}]}";
String jsonTest2 = "{\"results\":[{\"id\":\"19\",\"name\":\"Stuff\",\"child\":\"\"}]}";
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(ChildHolder.class, new ChildDeserializer());
Gson gson = gsonBuilder.create();
Response response1 = gson.fromJson(jsonTest1, Response.class);
Response response2 = gson.fromJson(jsonTest2, Response.class);
Also please read this link for further information.
回答2:
Another option for writing a custom serializer is to simply convert the empty string to an empty array in the JSON. Then your class can remain the way it is:
class ChildDeserializer implements JsonDeserializer<CategoryModel>
{
@Override
public ChildHolder deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context)
throws JsonParseException {
JsonObject obj = json.getAsObject();
JsonElement e = obj.get("child");
if (e.isJsonPrimitive()) // it's a String
{
obj.remove("child");
obj.add("child", new JsonArray());
}
return new Gson().fromJson(obj, CategoryModel.class);
}
}
来源:https://stackoverflow.com/questions/22142767/making-gsonrequest-to-accept-empty-list-or-null-array