5

I have a json object as I have shown in two cases :-

Case 1 :

     {

                 OWNER_ID : 145
     }

Case 2 :

     {

                  OWNER_ID : null
     }

Now in order to parse the data I am using the following statement :

int note_owner_id = jsonObject.getInt("OWNER_ID");

I am aware of the fact that in java we need to use wrapper class in order to extract a NULL integer and the code should be written like this:-

Integer note_owner_id = jsonObject.getInt("OWNER_ID");

But still I am not able to parse the data successfully. Can any one help me? How to parse the int value in general so that it won't show any Exception?

Thank you in advance.

2

6 Answers 6

13

Try this way, hope this will help you to solve your problem.

Instead of using getInt(String name) try to use optInt(String name) or optInt(String name,int fallBack) which will handle null value:

jsonObject.optInt("OWNER_ID");

Or

jsonObject.optInt("OWNER_ID", 0);
Sign up to request clarification or add additional context in comments.

5 Comments

so we need to use optInt, optString, optLong etc etc... instead of getInt, getString??? everywhere? –
@user3080161,yes you can and please check more details on given reference link.
@user3080161,you can approve any one of user ans which is given first hint to solve your problem.
what if i want null as fallback value?
Sadly, the class ignore the null return situation.But it indeed exists. Because the ignorance we have to check null or existence. it‘s not clean code.
3

You could set your Integer with a ternary and isNull(String)

Integer note_owner_id = (jsonObject.isNull("OWNER_ID")) ? null :
     jsonObject.getInt("OWNER_ID");

1 Comment

this works for me since i needed null as fallback value!
2

you may use optInt

public int optInt (String name)

Returns the value mapped by name if it exists and is an int or can be coerced to an int, or 0 otherwise.

to handle null values as

jsonObject.optInt("OWNER_ID");

or

jsonObject.optInt("OWNER_ID", defaultValue);

5 Comments

@user3080161 Happy to help,enjoy.
so we need to use optInt, optString, optLong etc etc... instead of getInt, getString??? everywhere?
@user3080161 you can,but then it will check for null for all values.
@user3080161,yes you can use any of them as per your requirement.
optInt should be then more preferred over getInt as it is a safer option rite?
0

just add below line for check your variable is null or not:-

if(jsonObject.isNULL("OWNER_ID"))
{
    // code here when data is null
}
else
{
    int note_owner_id = jsonObject.getInt("OWNER_ID");
}

Comments

0

You can use 'Opt' for Json when it's Nullable and thats easy. Instead of 'getInt' or 'getString' use:

int anything= json.optInt( "any", 0 );

Comments

0

Sadly, the class ignore the null return situation. But the situation indeed exists and null value is valid. Because of the ignorance we have to check null or key existence. Not clean code.

Very annoying and not friendly method design !

So I have to implement the custom class.

import org.json.JSONException;
import org.json.JSONObject;

public class JSONUtil {

    public static Integer optInt(JSONObject json, String key) {
        if (json.isNull(key)) {
           return null;
        } else {
            try {
                return json.getInt(key);
            } catch (JSONException e) {
                return null;
            }
        }
    }

    public static Long optLong(JSONObject json, String key) {
        if (json.isNull(key)) {
            return null;
        } else {
            try {
                return json.getLong(key);
            } catch (JSONException e) {
                return null;
            }
        }
    }

    public static Boolean optBoolean(JSONObject json, String key) {
        if (json.isNull(key)) {
            return null;
        } else {
            try {
                return json.getBoolean(key);
            } catch (JSONException e) {
                return null;
            }
        }
    }


    public static Double optDouble(JSONObject json, String key) {
        if (json.isNull(key)) {
            return null;
        } else {
            try {
                return json.getDouble(key);
            } catch (JSONException e) {
                return null;
            }
        }
    }

    public static String optString(JSONObject json, String key) {
        if (json.isNull(key)) {
            return null;
        } else {
            try {
                return json.getString(key);
            } catch (JSONException e) {
                return null;
            }
        }
    }


}

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.