If you need to add a property with a null
value, you should use the predefined static final JSONObject.NULL
and not the standard Java null
reference.
JSONObject.NULL
is a sentinel value used to explicitly define a property with an empty value.
JSONObject obj = new JSONObject();
obj.put("some", JSONObject.NULL); //Creates: {"some":null}
System.out.println(obj.get("some"));//prints: null
Note
JSONObject.NULL.equals(null); //returns true
Which is a clear violation of Java.equals()
contract:
For any non-null reference value x, x.equals(null) should return false