I have JSON with the following structure:
{"source":[ {"name":"john","age":20}, {"name":"michael","age":25}, {"name":"sara", "age":23} ] }
I named this JSON line as mainJSON . I am 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 am shown the following exception for line 2:
org.json.JSONException: JSONArray initial value should be a string or collection or array.
Tell me where I am making a mistake and how to fix it.
java json parsing
Amitava Chakraborty Apr 13 2018-11-11T00: 00Z
source share