My code extract results of JSONObject, but, sometimes, the i value don't begin to 1, and i have an error like that :
org.json.JSONException: No value for 1
My code :
JSONObject obj = new JSONObject(result);
for(int i=1;i<=14;i++) {
JSONArray arr = obj.getJSONArray(""+i);
extraction(arr, i);
}
I want to test before the extraction if the object code (i) exists or not. How i can do this ?
Return valueJsonObject::isNull() returns a bool that tells if the JsonObject points to something: true if the JsonObject is null, false if the JsonObject is valid and points to an object.
Return valueJsonArray::isNull() returns a bool that tells if the JsonArray points to something: true if the JsonArray is null, false if the JsonArray is valid and points to an array.
Try with json. isNull( "field-name" ) .
Null valuesJSON has a special value called null which can be set on any type of data including arrays, objects, number and boolean types.
use obj.optJSONArray(name)
the response will be null if the name does not exists.
JSONObject obj = new JSONObject(result);
for(int i=1;i<=14;i++) {
JSONArray arr = obj.optJSONArray(""+i);
if(arr != null) {
extraction(arr, i);
}
}
use JSONObject.optJSONArray(key).
As indicated in the documentation, it returns null in case the key is not present.
Also, your JSON structure seems weird. Why do you have numeric ordered keys in an object? shouldn't that be an Array?
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