I'm new to Java and I have a problem :
JSONObject data = new JSONObject(json);
List<String> list = (List<String>) data.getJSONArray("childrens");
for (String item : list) {
Log.d("DEBUG", item);
}
I would like to assign data.getJSONArray("childrens") to a variable list, but I have an error:
java.lang.ClassCastException: org.json.JSONArray cannot be cast to java.util.List
How can I do this?
I would like to iterate over JSON Array.
.getJSONArray(...)returns anorg.json.JSONArrayobject, not ajava.util.Listobject, and casting the thing won't fix this. You need to create a JSONArray variable, assign to it the object returned from the method, and then use methods available to this object.length()andget(int index)methods to iterate through using a for loop.