Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Gson JsonArray is empty

Tags:

json

gson

I am using HttpURLConnection to retrieve a JSON string. It looks like this:

{
  "status":"ok",
  "testSuites":[
    {
      // possibly one object in here
    }
  ]
}

I want to know if the array is empty. There will be at most one object in the array. I tried the following:

JsonParser parser = new JsonParser();
JsonObject obj = parser.parse(json).getAsJsonObject();
JsonArray testSuites = obj.getAsJsonArray("testSuites");

and then checked if testSuites was null, but that doesn't work, because it's not null. But it is empty!

like image 818
Alex Parker Avatar asked Feb 03 '16 00:02

Alex Parker


1 Answers

I figured it out. You can use the size() method to determine the number of elements in the array.

if (testSuites.size() == 0)
like image 188
Alex Parker Avatar answered Oct 08 '22 16:10

Alex Parker