How can i read a json array that contains a json array this is my json
{
"product": {
"name": "myApp",
"config": [
{
"grade": "elementary school",
"courses": [
{
"name": "Math",
"teacher": "David"
}
]
}
]
}
}
for example how can i read "config" and then courses, to generate a list that show me elementary school and then if i tap in that name my app show me the name of the course and the name of the teacher
Well first, this is not a JSONArray; it's a JSONObject. A JSONArray is denoted by opening and closes braces ([ and ], respectively), while a JSONObject is denoted by opening/closing brackets ({ and }, respectively).
Now, to answer your question as to how to parse it...
Let's assume you have:
String s = your_json_data;
Now, to parse that:
JSONObject jsonObj = new JSONObject(s);
JSONObject productJson = jsonObject.getJSONObject("product"); // May want to consider using optJSONObject for null checking in case your key/value combination doesn't exist
String name = productJson.getString("name"); // myApp
Now that should get you started with the basic stuff... Let's go over iterating through an actual JSONArray:
JSONArray configJsonArray = productJson.getJSONArray("config");
for(int configIterator = 0; configIterator < configJsonArray.length(); configIterator++){
JSONObject innerConfigObj = configJsonArray.getJSONObject(configIterator);
String configGrade = innerConfigObj.getString("grade");
JSONArray courseJsonArray = innerConfigObj.getJSONArray("courses");
for(int courseIterator = 0; courseIterator < courseJsonArray.length(); courseIterator++){
JSONObject innerCourseObj = innerCourseObj.getJSONObject(courseIterator);
String courseName = innerCourseObj.getString("name");
String courseTeacher = innerCourseObj.getString("teacher");
}
}
That should allow you to iterate through them.