I have some JSON with the following structure:
{"source":[ {"name":"john","age":20}, {"name":"michael","age":25}, {"name":"sara", "age":23} ] } I have named this JSON string as mainJSON. I'm trying to access the elements "name" and "age" with the following Java code:
JSONArray jsonMainArr = new JSONArray(mainJSON.getJSONArray("source")); for (int i = 0; i < jsonMainArr.length(); i++) { // **line 2** JSONObject childJSONObject = jsonMainArr.getJSONObject(i); String name = childJSONObject.getString("name"); int age = childJSONObject.getInt("age"); } I'm being shown the following exception for line number 2:
org.json.JSONException: JSONArray initial value should be a string or collection or array. Please guide me as to where I'm making the mistake and how to rectify this.