How to check that JSONArray is empty in java?

I am working on an android application that receives the json content of a web service called "WebUntis". The Json content I get looks like this:

{"jsonrpc":"2.0","id":"req-002", "result":[ {"id":125043,"date":20110117,"startTime":800,"endTime":850, "kl":[{"id":71}], "te":[{"id":23}], "su":[{"id":13}], "ro":[{"id":1}]}, {"id":125127,"date":20110117,"startTime":1055,"endTime":1145, "kl":[{"id":71}], "te":[{"id":41}], "su":[{"id":19}], "ro":[{"id":31}]}, ...]} 

As you can see in the results array, there are other arrays like "kl", "su" and "ro"

I get the contents of this array and then I store it in an arraylist. But when one of these arrays is empty, for example:

  {"jsonrpc":"2.0","id":"req-002", "result":[ {"id":125043,"date":20110117,"startTime":800,"endTime":850, "**kl":[]**, "te":[{"id":23}], "su":[{"id":13}], "ro":[{"id":1}]}, {"id":125127,"date":20110117,"startTime":1055,"endTime":1145, "kl":[{"id":71}], "te":[{"id":41}], "su":[{"id":19}], "ro":[{"id":31}]}, ...]} 

I always get an IndexOutOfRangeException error, but I always say that it should not take empty arrays, this is what I tried:

  JSONObject jsonResult = new JSONObject(s); // Get the result object JSONArray arr = jsonResult.getJSONArray("result"); for (int i = 0; i < arr.length(); i++) { JSONObject c = arr.getJSONObject(i); anfangStunde[i] = c.getString("startTime"); endeStunde[i] = c.getString("endTime"); // get the jsonarrays (kl, su, ro) kl = c.getJSONArray("kl"); su = c.getJSONArray("su"); ro = c.getJSONArray("ro"); // check if kl is not null if(kl != null){ klassenID[i] = kl.getJSONObject(0).getString("id"); } if (klassenID[i] != null) { klasse = webuntis.klassenMap.get(klassenID[i]); Log.d("ID und Klasse=", "" + klassenID[i] + ";" + klasse); } // get th ids fachID[i] = su.getJSONObject(0).getString("id"); if (fachID[i] != null) { fach = webuntis.faecherMap.get(fachID[i]); Log.d("ID und Fach=", "" + fachID[i] + ";" + fach); } // "Start;Ende;Klasse;Fach;Raum" store in arraylist webuntis.stundenPlan.add(anfangStunde[i] + ";" + endeStunde[i] + ";" + klasse + ";" + fach); // Write Data into a file for offline use: } 

Can anybody help me?

+17
json arrays json-rpc
source share
2 answers

If the array is defined in the file but empty, for example:

 ... "kl":[] ... 

Then getJSONArray("kl") will return an empty array, but the object is not null . Then if you do this:

 kl = c.getJSONArray("kl"); if(kl != null){ klassenID[i] = kl.getJSONObject(0).getString("id"); } 

kl not null and kl.getJSONObject(0) throw an exception - there is no first element in the array.

Instead, you can check length() , for example:

 kl = c.getJSONArray("kl"); if(kl != null && kl.length() > 0 ){ klassenID[i] = kl.getJSONObject(0).getString("id"); } 
+31
source share

You can also use the isEmpty() method, which we use to check if the list is empty or not. This method returns a boolean value. Returns true if the list is empty, otherwise returns false. For example:

 if (!k1.isEmpty()) { klassenID[i] = kl.getJSONObject(0).getString("id"); } 
+1
source share

All Articles