Reputation: 727
JSON for Example:
{
"example_key1": {
"one": 1,
"two": 2,
"three": 3
},
"example_key2": [
{
"four": 4,
"five": 5,
"six": 6
}
]
}
Right now i am consuming one method from web service.That method is returning some JSON data like the above JSON example.
Here my problem is if some KEY values is missing from the JSON data, after consuming that method.(Say "example_key2" json value from the above json example is missing)
in the sense,
how can i recognize wether that key value is available or not?
Upvotes: 0
Views: 151
Reputation: 3255
Use has method of JSONObject class
http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)
Returns true if this object has a mapping for name. The mapping may be NULL.
Upvotes: 1
Reputation: 128428
You can check whether particular key is available or not by using has() method
For example:
if(myJSONObject.has("one")) {
num = myJSONObject.optString("one");
}
Upvotes: 1