Reputation: 135
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.
Upvotes: 5
Views: 7783
Reputation: 4246
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;
}
}
}
}
Upvotes: 0
Reputation: 318
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 );
Upvotes: 0
Reputation: 24848
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);
Upvotes: 13
Reputation: 14398
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);
Upvotes: 2
Reputation: 201439
You could set your Integer
with a ternary and isNull(String)
Integer note_owner_id = (jsonObject.isNull("OWNER_ID")) ? null :
jsonObject.getInt("OWNER_ID");
Upvotes: 3
Reputation: 38429
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");
}
Upvotes: 0