JSONArray Object Empty or Null (JOEN)

Hello guys, this time I want to share about the case when I first played with json objects in Android Studio. Let's say I'm working on an android application that gets json content from a web service called "WebUntis". The Json content 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, there are also other arrays like "id", "kl", "te", "su" and "ro". When the content of those arrays contains data, then I can smoothly store them into an ArrayList. But when even one of those arrays is empty, like this (note "kl"),

{"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}]},
]}

So what happened was I always got an error: IndexOutOfRangeException, so the temporary solution at that time was I asked the web division to give a special value if there was no data, I know this is very silly, because I'm just learning. This is roughly what the code was like at that time.

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:
 }

Of course I didn't just leave it like that, as time went on I continued to improvise on the code and the point is,

1. If the array is defined in an empty file, such as.

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

2. Then getJSONArray("kl") will return an array with the value "empty", but you need to be aware that the array does not mean "null". So, if you do this.

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

So it is no wonder if you always get this error. The solution is to use length(), instead.

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

Remember! EMPTY != NULL

Done!, now your code is a little bit better


Post a Comment

Previous Next

نموذج الاتصال