I'm new to Java coding and I want to parse json response, it looks like this:
{
"13.12.2021 Monday": {
"lessons": [
{
"type": "second",
"lesson":{
"subject": "ОР и АБД",
"teacher": "Кузьмина В.М.",
"room": null
}
},
{
"type": "common",
"0": {
"subject": "ПМ.2.Р5. РС и ПИР со С и ДК",
"teacher": "Голубева Я.С.",
"room": "211"
}
}
],
"dinner": "11.00 "
},
"14.12.2021 Tuesday": {
"lessons":[
{
"type": "common",
"0":{
"subject": "ПМ.2.Р1. ТРОПО",
"teacher": "Ахметова Ф.Т.","room":"212"
}
},
{
"type": "common",
"0":{
"subject": "ПМ.2.Р1. ТРОПО",
"teacher": "Ахметова Ф.Т.",
"room": "212"
}
}
],
"dinner": "11:00 "
}
}
I use this code
JSONObject json = new JSONObject(result);
Iterator<String> days = json.keys();
String response = "";
while(days.hasNext()) {
String key = days.next();
JSONObject day = json.getJSONObject(key);
JSONArray lessons = day.getJSONArray("lessons");
int lessonsCount = lessons.length(), i;
for (i = 0; i < lessonsCount; i = i 1) {
JSONObject lesson = lessons.getJSONObject(i);
response = lesson.getString("type") '\n';
}
response = '\n';
}
But get this error:
org.json.JSONException: Value [] at 4 of type org.json.JSONArray cannot be converted to JSONObject
Please, explain me how can I access all the child elements such as "lesson" block and it's "subject", "teacher", "room". I've checked similar questions but none helped me
CodePudding user response:
For example:
StringBuilder response = new StringBuilder();
while (days.hasNext()) {
String key = days.next();
JSONObject day = json.getJSONObject(key);
JSONArray lessons = day.getJSONArray("lessons");
for (int i = 0; i < lessons.length(); i ) {
JSONObject lesson = lessons.getJSONObject(i);
Iterator<String> item = lesson.keys();
while (item.hasNext()) {
String jKey = item.next();
JSONObject jsonObject = lesson.optJSONObject(jKey);
if (jsonObject != null) {
parse(response, jsonObject);
} else {
if (lesson.get(jKey) != null) {
response.append(lesson.getString(jKey)).append("\n");
}
}
}
}
response.append('\n');
}
private void parse(StringBuilder response, JSONObject jsonObject) {
if (jsonObject != null) {
Iterator<String> item = jsonObject.keys();
while (item.hasNext()) {
String jKey = item.next();
JSONObject jObject = jsonObject.optJSONObject(jKey);
if (jObject != null) {
parse(response, jsonObject);
} else {
response.append(jsonObject.get(jKey)).append("\n");
}
}
}
}
CodePudding user response:
I updated your provided function to have your required fields
while(days.hasNext()) {
String key = days.next();
JSONObject day = json.getJSONObject(key);
JSONArray lessons = day.getJSONArray("lessons");
int lessonsCount = lessons.length(), i;
for (i = 0; i < lessonsCount; i = i 1) {
JSONObject lesson = lessons.getJSONObject(i);
Iterator<String> lessonKeys = lesson.keys();
while(lessonKeys.hasNext()) {
String lessonKey = lessonKeys.next();
if(!lessonKey.equals("type")) {
JSONObject lessonData = lesson.getJSONObject(lessonKey);
response = lessonData.getString("subject") " - " lessonData.getString("teacher") " - " lessonData.get("room") '\n';
}
}
}
}
The output as follow:
ПМ.2.Р1. ТРОПО - Ахметова Ф.Т. - 212
ПМ.2.Р1. ТРОПО - Ахметова Ф.Т. - 212
ОР и АБД - Кузьмина В.М. - null
ПМ.2.Р5. РС и ПИР со С и ДК - Голубева Я.С. - 211