3 回答
![?](http://img1.sycdn.imooc.com/533e4c1500010baf02200220-100-100.jpg)
TA贡献1735条经验 获得超5个赞
如果您正在使用,则在 if 检查大小的内部 org.json.simple.JSONArray
if(array.size() != 0)
或者如果你使用 org.json.JSONArray
if(array.length() != 0)
![?](http://img1.sycdn.imooc.com/5333a2320001acdd02000200-100-100.jpg)
TA贡献1848条经验 获得超2个赞
您不需要使用 if 条件,因为 java 的 for-each 足够聪明来处理这个问题。
如果数组没有任何值或者它是空的,那么它根本不会被执行。
所以使用这个代码:
for (final JsonElement json : array) { // it will not get executed at all if array is empty
JSONObject jsonObject = new JSONObject(String.valueOf(json));
System.out.println("Inside fromJasonToJava, jsonObject:" + jsonObject);
代替
if(array.get(0)!= null) { //It goes wrong here, I need a working "if" condition
//Value is not null
for (final JsonElement json : array) {
JSONObject jsonObject = new JSONObject(String.valueOf(json));
System.out.println("Inside fromJasonToJava, jsonObject:" + jsonObject);
}
添加回答
举报