get(int index)
Get the object value associated with an index.
opt(int index)
Get the optional object value associated with an index.
What is the optional object
or array
?
get(int index)
Get the object value associated with an index.
opt(int index)
Get the optional object value associated with an index.
What is the optional object
or array
?
get(index) throws JSONException if the index isn't found where opt stand for optional and can be used for values that are optional in the JSONObject and there are good chances that it might not exist in some scenarios.
For ex. you have a JSONArray with 10 JSONObjects in it and 3 of your JSONObjects contains a value or index that might not exist in rest 7 JSONObject. In this scenario, instead of writing two different JSON parsers you can just simply use opt for the optional values and can use the same parser to parse all the JSONObjects in the array.
Hope it helps.
get throws a JSONException if the Object associated with "index" doesn't exist or is null.
opt returns null, instead.
so here "optional" means that that object or array may not exist