JSONObject、JSONArray 非空判断

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"));
}

 

文章链接:JsonObject、JsonArray的封装、打印及非空判断

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章