最佳答案
如何检测 json 值是否为 null? 例如: < strong > [{“ username”: null } ,{“ username”: “ null”}]
第一种情况表示一个不存在的用户名,第二种情况表示一个名为“ null”的用户。但是如果你试图检索它们,两个值都会导致字符串“ null”
JSONObject json = new JSONObject("{\"hello\":null}");
json.put("bye", JSONObject.NULL);
Log.e("LOG", json.toString());
Log.e("LOG", "hello="+json.getString("hello") + " is null? "
+ (json.getString("hello") == null));
Log.e("LOG", "bye="+json.getString("bye") + " is null? "
+ (json.getString("bye") == null));
日志输出为
{"hello":"null","bye":null}
hello=null is null? false
bye=null is null? false