测试getJSONArray是否为null [英] Test if getJSONArray is null or not
本文介绍了测试getJSONArray是否为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码提取了JSONObject的结果,但是有时i的值不会开始为1,并且我有这样的错误:
My code extract results of JSONObject, but, sometimes, the i value don't begin to 1, and i have an error like that :
org.json.JSONException: No value for 1
我的代码:
JSONObject obj = new JSONObject(result);
for(int i=1;i<=14;i++) {
JSONArray arr = obj.getJSONArray(""+i);
extraction(arr, i);
}
我想在提取之前测试目标代码(i)是否存在.我该怎么做?
I want to test before the extraction if the object code (i) exists or not. How i can do this ?
推荐答案
使用obj.optJSONArray(name)
如果名称不存在,则响应为null.
use obj.optJSONArray(name)
the response will be null if the name does not exists.
JSONObject obj = new JSONObject(result);
for(int i=1;i<=14;i++) {
JSONArray arr = obj.optJSONArray(""+i);
if(arr != null) {
extraction(arr, i);
}
}
这篇关于测试getJSONArray是否为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文