praveen kashyap
praveen kashyap

Reputation: 11

iterate through JSONArray in java

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?

Upvotes: 1

Views: 7385

Answers (1)

Krishnanunni P V
Krishnanunni P V

Reputation: 699

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
}

Upvotes: 3

Related Questions