如何检查Java中的JSONArray是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28807919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to check if a JSONArray is empty in java?
提问by seriously
I am working on an android app that get json content of a webservice called "WebUntis". The Json content i am getting looks like:
我正在开发一个 android 应用程序,它获取名为“WebUntis”的网络服务的 json 内容。我得到的 Json 内容如下:
{"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 result-array there are also other arrays like "kl", "su" and "ro"
正如您在结果数组中看到的,还有其他数组,如“kl”、“su”和“ro”
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}]},
...]}
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:
我总是收到错误IndexOutOfRangeException,但我总是告诉它不应该使用空数组,这是我尝试过的:
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 anyone help me ?
谁能帮我 ?
回答by T.Gounelle
If the array is defined in the file butis empty, like:
如果数组在文件中定义但为空,例如:
...
"kl":[]
...
Then getJSONArray("kl")will return an emptyarray, but the object is not null. Then, if you do this:
然后getJSONArray("kl")将返回一个空数组,但对象不是null。然后,如果你这样做:
kl = c.getJSONArray("kl");
if(kl != null){
klassenID[i] = kl.getJSONObject(0).getString("id");
}
klis notnulland kl.getJSONObject(0)will throw an exception - there is no first element in the array.
kl是不null与kl.getJSONObject(0)将抛出异常-有数组中没有第一个元素。
Instead you can check the length(), e.g.:
相反,您可以检查length(),例如:
kl = c.getJSONArray("kl");
if(kl != null && kl.length() > 0 ){
klassenID[i] = kl.getJSONObject(0).getString("id");
}
回答by abbu jan
You can also use isEmpty()method, this is the method we use to check whether the list is empty or not. This method returns a Boolean value. It returns true if the list is empty otherwise it gives false. For example:
您也可以使用isEmpty()方法,这是我们用来检查列表是否为空的方法。此方法返回一个布尔值。如果列表为空,则返回 true,否则返回 false。例如:
if (!k1.isEmpty()) {
klassenID[i] = kl.getJSONObject(0).getString("id");
}
回答by DragonFire
This is another way to do it...
这是另一种方式来做到这一点......
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call,
Response<JsonObject> response) {
JSONObject jsonObject;
try {
jsonObject = new JSONObject(new Gson().toJson(response.body()));
JSONArray returnArray = jsonObject.getJSONArray("my_array");
// Do Work Only If Not Null
if (!returnArray.isNull(0)) {
for (int l = 0; l < returnArray.length(); l++) {
if (returnArray.length() > 0) {
// Get The Json Object
JSONObject returnJSONObject = returnArray.getJSONObject(l);
// Get Details
String imageUrl = returnJSONObject.optString("image");
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<JsonObject> call,
Throwable t) {
}
});

