If you're using the built-in Android JSON library, then you're actually using the (quite capable) libraries from org.json.
https://github.com/douglascrockford/JSON-java
If that's the case, we can just grep the code to see most of the answers here.
In particular, I am confused as to whether the JSONObject.NULL object means the same thing as null?
This should hopefully help:
/**
* JSONObject.NULL is equivalent to the value that JavaScript calls null,
* whilst Java's null is equivalent to the value that JavaScript calls
* undefined.
*/
private static final class Null {
/**
* There is only intended to be a single instance of the NULL object,
* so the clone method returns itself.
* @return NULL.
*/
protected final Object clone() {
return this;
}
/**
* A Null object is equal to the null value and to itself.
* @param object An object to test for nullness.
* @return true if the object parameter is the JSONObject.NULL object
* or null.
*/
public boolean equals(Object object) {
return object == null || object == this;
}
/**
* Get the "null" string value.
* @return The string "null".
*/
public String toString() {
return "null";
}
}
/**
* It is sometimes more convenient and less ambiguous to have a
* <code>NULL</code> object than to use Java's <code>null</code> value.
* <code>JSONObject.NULL.equals(null)</code> returns <code>true</code>.
* <code>JSONObject.NULL.toString()</code> returns <code>"null"</code>.
*/
public static final Object NULL = new Null();
Part 2:
am not sure how to determine whether this JSONObject instance has a
mapping for "mykey" and that the value of the mapping is null? Would
the following check do it...
myJSONObject.has("mykey") && !myJSONObject.isNull("mykey")
Yep - that seems to be about as good a method as any. In particular, you want to check for null using the isNull method, as JSONObject defines it's own NULL object (see above).
In addition: as well as getting mappings of keys whose value can be
null, I need to create JSONObject instances that map null to keys.
Would...
myJSONObject.put("mykey", JSONObject.NULL); ... do the job, or is
there another way I should be doing this?
That's exactly how you want to do it - in particular, using JSONObject.NULL rather than java's null (see above again).