用 Java 解析 JSON 多维数组
Posted
技术标签:
【中文标题】用 Java 解析 JSON 多维数组【英文标题】:Parse JSON multidimensional array with Java 【发布时间】:2022-01-19 17:27:36 【问题描述】:我是 Java 编码的新手,我想解析 json 响应,它看起来像这样:
"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 "
我用这个代码
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';
但是得到这个错误:
org.json.JSONException: Value [] at 4 of type org.json.JSONArray 无法转换为 JSONObject
请解释我如何访问所有子元素,例如“课程”块,它是“主题”、“老师”、“房间”。 我检查过类似的问题,但没有一个对我有帮助
【问题讨论】:
你在哪一行得到这个错误?JSONObject lesson = lessons.getJSONObject(i);
@Mike'Pomax'Kamermans
请记得说in your post。
【参考方案1】:
例如:
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");
【讨论】:
【参考方案2】:我更新了您提供的功能以包含您的必填字段
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';
输出如下:
ПМ.2.Р1. ТРОПО - Ахметова Ф.Т. - 212
ПМ.2.Р1. ТРОПО - Ахметова Ф.Т. - 212
ОР и АБД - Кузьмина В.М. - null
ПМ.2.Р5. РС и ПИР со С и ДК - Голубева Я.С. - 211
【讨论】:
以上是关于用 Java 解析 JSON 多维数组的主要内容,如果未能解决你的问题,请参考以下文章