1

I am making calls to a server which can return key-value mappings in the following format:

{...
 "mykey":null
...}

I am able to create a JSONObject instance from the above (using the JSONObject(String json) constructor) but 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")

... or is there a better way? In particular, I am confused as to whether the JSONObject.NULL object means the same thing as null?

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?

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Hasta la vista, baby.
1

Have a look at the API docs.

boolean has(String name)
         Returns true if this object has a mapping for name.

boolean isNull(String name)
         Returns true if this object has no mapping for name or if it has a mapping whose value is NULL.

If you are not sure, how the serializer / deserializer behaves, you can always test the output / parsing results with simple code snippets.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.