1

I have a JSONArray :

public JSNArray getItems() {
  JSONArray listjson = new JSONArray();
  JSONObject fnl_json = new JSONObject(); 
  //adding few items to fnl_json 
  listjson.add(fnl_json); 
  return fnal_json; 
}

I am calling getItems function, how can I iterate through fnl_json from the called function?

1

1 Answer 1

3

To iterate a JSONArray you can do:

JSONObject jsonObject = new JSONObject("jsonString");
JSONArray jsonArray = jsonObject.getJSONArray("arrayName");
for (int i = 0; i < jsonArray.length(); i++) {
   JSONObject explrObject = jsonArray.getJSONObject(i); // you will get the json object
   //do the stuff
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.