Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...