Java 中判斷 JSONObject 對應的 VALUE 爲空

目前發現有兩種包.兩種不同的json包.
第一種狀況是: json包是json-lib包是net.sf.json

怎樣判斷JSONObject返回的是字符串null仍是null值.json

研究源碼發現.JSONObject裏有一段代碼是當遇到json串中是null的時候.返回JSONNUll.
因此區分null時這樣:
JSONObject jo = JSONObject.fromObject("{a:null,b:\"null\"}");
Object o = jo.get("a");
if(o instanceof JSONNull){
System.out.println("Is empty null");
}else{
System.out.println("is String null");
}
o = jo.get("b");
if(o instanceof JSONNull){
System.out.println("Is empty null");
}else{
System.out.println("is String null");
}
輸入的結果爲
 
第二種狀況是: org.json的包
 
JSONObject jo = new JSONObject(("{\"a\":\"null\",\"b\":null}"));
if(jo.get("a") instanceof String){
System.out.println("a is String null");
}else{
System.out.println("a is empty null");
}
if(jo.get("b") instanceof String){
System.out.println("a is String null");
}else{
System.out.println("a is empty null");
}
System.out.println(jo.get("b").getClass());
這時候發現.返回的null是JSONObject.NUll
兩種包不同的返回NUll值
相關文章
相關標籤/搜索