JSONObject、JSONArray 非空判斷方法
如果能保證 JSONObject、JSONArray 不是null,
//JSONObject空判斷
if(jsonObjB.isEmpty()){
System.out.println("jsonObjB爲空");
}
//JSONObject非空判斷
if(!jsonObjB.isEmpty()){
System.out.println("jsonObjB爲非空");
}
//JSONArray空判斷
if(jsonArrB.size()<1){
System.out.println("jsonArr爲空");
}
//JSONArray非空判斷
if(jsonArrB.size()>0){
System.out.println("jsonArr爲非空");
}
如果不確定 JSONObject、JSONArray 是不是null,
注意:null.isEmpty() 和 null.size() 會報空指針異常,但是因爲 || 或 &&,走不到這一步,所以下面的公式不會報錯。
//JSONObject空判斷
if(jsonObjB == null || jsonObjB.isEmpty()){
System.out.println("jsonObjB爲空");
}
//JSONObject非空判斷
if(jsonObjB != null && !jsonObjB.isEmpty()){
System.out.println("jsonObjB爲非空");
}
//JSONArray空判斷
if(jsonArrB==null || jsonArrB.size()<=0){
System.out.println("jsonArr爲空");
}
//JSONArray非空判斷
if(jsonArrB!=null && jsonArrB.size()>0){
System.out.println("jsonArr爲空");
}
驗證取值方式
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class TEST {
public static void main(String[] args) {
String str = "{'res':'', 'msg':{}, 'data1':[], 'data2':[{'Name':'張三', 'Tel':'123'},{'Name':'李四', 'Tel':'456'} ]}";
JSONObject jsonObj = JSONObject.fromObject(str);
//下面這種取值方式,報錯JSONObject["res"] is not a JSONObject,
//JSONObject jsonObject = jsonObj.getJSONObject("res");
//下面這兩種方式能取到對象,但是等同於 new 對象
JSONObject jsonObject = jsonObj.getJSONObject("msg"); //即jsonObject是{}
JSONArray jsonArray = jsonObj.getJSONArray("data1"); //即jsonArray是[]
}
}
驗證各種判斷方法
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class TEST {
public static void main(String[] args) {
JSONObject jsonObjA = null;
JSONObject jsonObjB = new JSONObject(); //即jsonObjB是{}
if(jsonObjA == null){
System.out.println("jsonObjA == null 判斷成功");//成功
}
//空指針異常
/* if(jsonObjA.isEmpty()){
System.out.println("jsonObjA.isEmpty() 判斷成功");
}
if(jsonObjA.isNullObject()){
System.out.println("jsonObjA.isNullObject() 判斷成功");
} */
if("null".equals(jsonObjA)){
System.out.println("'null'.equals(jsonObjA) 判斷成功");//失敗
}
if(jsonObjB == null){
System.out.println("jsonObjB == null 判斷成功");//失敗
}
if(jsonObjB.isEmpty()){
System.out.println("jsonObjB.isEmpty() 判斷成功");//成功
}
if(jsonObjB.isNullObject()){
System.out.println("jsonObjB.isNullObject() 判斷成功");//失敗
}
if("null".equals(jsonObjB)){
System.out.println("'null'.equals(jsonObjB) 判斷成功");//失敗
}
//JSONObject空判斷
if(jsonObjB == null || jsonObjB.isEmpty()){
System.out.println("jsonObjB爲空");
}
//JSONObject非空判斷
if(jsonObjB != null && !jsonObjB.isEmpty()){
System.out.println("jsonObjB爲非空");
}
JSONArray jsonArrA = null;
JSONArray jsonArrB = new JSONArray(); //即jsonArrB是[]
//空指針異常
/* if(jsonArrA.isEmpty()){
System.out.println("jsonArrA.isEmpty() 判斷成功");
}
if(jsonArrA.size()<1){
System.out.println("jsonArrA.size()<1 判斷成功");//
} */
if(jsonArrB.isEmpty()){
System.out.println("jsonArr.isEmpty() 判斷成功");//成功
}
if(jsonArrB.size()<=0){
System.out.println("jsonArr.size()<=0 判斷成功");//成功
}
//JSONArray空判斷
if(jsonArrB==null || jsonArrB.size()<=0){
System.out.println("jsonArr爲空");
}
//JSONArray非空判斷
if(jsonArrB!=null && jsonArrB.size()>0){
System.out.println("jsonArr爲空");
}
}
}
從上面的實驗中可以看到,jsonObj.isNullObject() 和 "null".equals(jsonObj) 根本不起作用!
"null".equals(jsonObj) 更是瞎扯淡!
實際使用案例
JSONObject / JSONArray 如果爲空,取值操作如 getJSONObject() 會報錯,所以要進行判斷:
正常數據:
resultJsonObj:{"itsmStatistics" : [ {TOTAL_NUM:12, TOTAL_PAGE:2 }, {TOTAL_NUM:22, TOTAL_PAGE: 3} ] },
如果數據爲空,即resultJsonObj:{"itsmStatistics" : [ ] },那麼下面代碼會報錯,所以要先進行判斷,避免報錯。
JSONArray sharedDetailData = resultJsonObj.getJSONArray("itsmStatistics");
if(sharedDetailData.size()>0){
pageInfo.put("totalNum", sharedDetailData.getJSONObject(0).getInt("TOTAL_NUM"));
pageInfo.put("totalPage", sharedDetailData.getJSONObject(0).getInt("TOTAL_PAGE"));
}