如何在 java 中使用 org.json.JSONObject 将值设置为 null?我当然可以通过使用 isNull 读取值是否为 null - 但似乎当我输入 null 时,它只是忽略了我:
JSONObject o = new JSONObject();
o.put("key",null);
o.isNull("key"); // returns true, buuuut...
o.has("key"); // returns false
o.isNull("somethingIHaventSetAtAll"); // also returns true, unfortunately
最佳答案
尝试设置JSONObject.NULL
而不是 null
:
A sentinel value used to explicitly define a name with no value. Unlike null, names with this value:
- show up in the names() array
- show up in the keys() iterator
- return true for has(String)
- do not throw on get(String)
- are included in the encoded JSON string.
This value violates the general contract of equals(Object) by returning true when compared to null. Its toString() method returns "null".
关于java - 如何在 java 中使用 org.json.JSONObject 将值设置为 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13613754/