Phate
Phate

Reputation: 6622

Setting a default value for empty json field

I have to parse a json whom a field can be empty:

{"fullField":"ok","canBeEmpty":""}

if I try to parse this string overall parsing fails with a "no value for canBeEmpty". For each json item I execute:

json_data.getString("field"); //throws exception if empty

I'd like to still keep the parsing, setting the canBeEmpty value to a default string...is it possibile?

Upvotes: 1

Views: 5017

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

you can use JSONObject.JSONObject(String name) to check if any name is exist or not in json object as:

if(JSONObject.isNull("field")){
// do something here
}
else{
//do something here
}

Upvotes: 4

Related Questions