I am working on an android app that get json content of a webservice called \"WebUntis\". The Json content i am getting looks like:
{\"jsonrpc\":\"2.0\",\"id
If the array is defined in the file but is empty, like:
...
"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
is not null
and kl.getJSONObject(0)
will throw an exception - there is no first element in the array.
Instead you can check the length()
, e.g.:
kl = c.getJSONArray("kl");
if(kl != null && kl.length() > 0 ){
klassenID[i] = kl.getJSONObject(0).getString("id");
}