I am using gson to parse a JSON reply. The code works fine for proper JSON response. However if the JSON reply is empty array, The my code keeps complaining "Was expecting begin_object but got end_array"
JSON response
{
"list" : {
"data" : [
]
}
}
My code
try {
jsonReader.beginArray();
do{
jsonReader.beginObject();
while(jsonReader.hasNext()){
// Parse all data
jsonReader.endObject();
} while(jsonReader.hasNext());
jsonReader.endArray();
} catch (IOException e) {
//Exception
}
I know what the above exception mean, It simply means it was expecting object inside an array to process. But since it is an empty array it gives exception.
But i have looked at the api guide, and there are no methods to check whether the JSON is an empty array or the next object in input stream is object or end of array etc.
Could any one tell me any such methods exist in GSON API. Or how we can over come this issue?
EDIT: I have modified the response i get from the server.
{[]}? Because that's not valid JSON... something like this:{"list":[]}sounds better... if it's the latter I can help you...