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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With