如何检查JSONArray在Java中是否为空? [英] how to check if a JSONArray is empty in java?
问题描述
我正在开发一个Android应用,该应用获取名为"WebUntis"的Web服务的json内容.我得到的Json内容如下:
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":"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}]},
...]}
在结果数组中可以看到,还有其他数组,例如"kl","su"和"ro"
As you can see in the result-array there are also other arrays like "kl", "su" and "ro"
我正在获取这些数组的内容,然后将它们存储在arraylist中. 但是当这些数组之一为空时,就像;
I am getting the content of these array and then i store them in an arraylist. But when one of these array is empty, like;
{"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}]},
...]}
我总是收到错误 IndexOutOfRangeException
,
但是我总是告诉它不应该使用空数组,这是我尝试过的:
I am always getting the error IndexOutOfRangeException
,
but I am always telling it that it should not take the empty arrays, this is what I have 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:
}
有人可以帮助我吗?
推荐答案
如果数组在文件中定义,但为空,例如:
If the array is defined in the file but is empty, like:
...
"kl":[]
...
然后getJSONArray("kl")
将返回一个 empty 数组,但该对象不是null
.然后,如果您这样做:
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
不是 null
,kl.getJSONObject(0)
将引发异常-数组中没有第一个元素.
kl
is not null
and kl.getJSONObject(0)
will throw an exception - there is no first element in the array.
相反,您可以检查length()
,例如:
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");
}
这篇关于如何检查JSONArray在Java中是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!