如何检测 json 值何时为空? 例如:[{"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
最佳答案
试试 json.isNull("field-name")
.
引用:http://developer.android.com/reference/org/json/JSONObject.html#isNull%28java.lang.String%29
关于java - Android Json 和空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10588763/