JSON jsonObject.optString() returns String "null"

Got rid off this situation by simply replacing "null" with "".

String city = address.optString("city").replace("null", "");

You're not alone in running into this problem and scratching your head, thinking "Could they really have meant this?" According to an AOSP issue, the Google engineers did consider this a bug, but they had to be compatible with the org.json implementation, even bug-compatible.

If you think about it, it makes sense, because if the same code which uses the same libraries run in other Java environments behaves differently in Android, there would be major compatibility problems when using 3rd party libraries. Even if the intentions were good and it truly fixed bugs, it would open up a whole new can of worms.

According to the AOSP issue:

The behavior is intentional; we went out of our way to be bug-compatible with org.json. Now that that's fixed, it's unclear whether we should fix our code as well. Applications may have come to rely on this buggy behavior.

If this is causing you grief, I recommend you workaround by using a different mechanism to test for null, such as json.isNull().

Here's a simple method to help you out:

/** Return the value mapped by the given key, or {@code null} if not present or null. */
public static String optString(JSONObject json, String key)
{
    // http://code.google.com/p/android/issues/detail?id=13830
    if (json.isNull(key))
        return null;
    else
        return json.optString(key, null);
}

You basically have 2 choices:

1) Send a JSON payload with null values

{
"street2": "s2",
"province": "p1",
"street1": null,
"postalCode": null,
"country": null,
"city": null
}

You will have to check for null values and parse them accordingly:

private String optString_1(final JSONObject json, final String key) {
    return json.isNull(key) ? null : json.optString(key);
}

2) Do not send the keys with null values and use optString(key, null) directly (should save you bandwidth).

{
"street2": "s2",
"province": "p1"
}

Tags:

Java

Json

Android