目前發現有兩種包.兩種不一樣的json包.
第一種情況是: json包是json-lib包是net.sf.json
怎樣判斷JSONObject返回的是字符串null還是null值.
研究源碼發現.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值