Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONArray Exception : Index 50 out of range [0..50). Is there any limit on JsonArray

Tags:

java

json

android

I'm using JSON in my android application in the following manner:

  • Send request to a URL & receive JSON response.
  • Parse JSON response & fetch the required element "results" which is a JSON array.
  • Loop on every i'th element of this JSON array and continue with the required operation

Code:

Integer numberOfItemsInResp = pagination.getInt("items");
    JSONArray results = jsonResponse.getJSONArray("results");
    for (int i = 0; i < numberOfItemsInResp; i++){
        JSONObject perResult = results.getJSONObject(i);
    }

Problem is when the i reaches 50, then JSONObject perResult = results.getJSONObject(i) throws "org.json.JSONException: Index 50 out of range [0..50)" Exception.

Is there any limitation attached to JSONArray?

like image 928
reiley Avatar asked Dec 04 '25 17:12

reiley


1 Answers

What is numberOfItemsInResp? Suggest you do this:

JSONArray results = jsonResponse.getJSONArray("results");
final int numberOfItemsInResp = results.length();
for (int i = 0; i < numberOfItemsInResp; i++){
    JSONObject perResult = results.getJSONObject(i);
}
like image 194
weston Avatar answered Dec 06 '25 07:12

weston