java - 如何将多个列表放入一个列表中?
问题描述
好的,所以我需要使用 Java 将许多不同的列表放入一个列表中。这是重要的代码。
String jsonText = buffer.toString(); // gets what the URL returns as JSON
JSONObject obj = new JSONObject(jsonText); // using JSONObject to pass to a JSONArray to search for the JSON
List<List<String>> allInfo = new ArrayList();// list to put all the returned information
List<String> innerList = new ArrayList();
JSONArray linemanques = obj.getJSONArray("linemanques"); //selects the array to read from
for (int i = 0; i < linemanques.length(); i++) {
JSONObject questionParts = linemanques.getJSONObject(i);
quesnum = questionParts.getString("quesnum"); // all of questionParts.getString() are for getting the data in the JSONArray
questype = questionParts.getString("questype");
question = questionParts.getString("question");
ans1 = questionParts.getString("ans1");
ans2 = questionParts.getString("ans2");
ans3 = questionParts.getString("ans3");
ans4 = questionParts.getString("ans4");
correctans = questionParts.getString("correctans");
category = questionParts.getString("category");
notes = questionParts.getString("notes");
flag = questionParts.getString("flag");
innerList.add(quesnum);
innerList.add(questype);
innerList.add(question);
innerList.add(ans1);
innerList.add(ans2);
innerList.add(ans4);
innerList.add(correctans);
innerList.add(category);
innerList.add(notes);
innerList.add(flag);
allInfo.add(innerList);
}
return allInfo;
如上所示,我将一个名为innerList 的List 放入另一个List 中,该List 的类型为名为allInfo 的List。在 for 循环中,我获取正在使用的 JSONArray 的长度,并将元素添加到 innerList。然后,我将 innerList 添加到 allInfo 中,然后将其返回。
顺便说一下,这里是 JSON。
{
"error": false,
"message": "Request successfully completed",
"linemanques": [
{
"quesnum": 1,
"questype": 1,
"question": "This is question #1",
"ans1": "This is answer 1",
"ans2": "This is answer 2\r\n",
"ans3": "This is answer 3\r\n\r\n\r\n",
"ans4": "This is answer 4",
"correctans": "ans4",
"notes": "This is a note",
"category": "2",
"flag": "ans4"
},
{
"quesnum": 2,
"questype": 2,
"question": "This is question #2",
"ans1": "This is Q2 ans 1",
"ans2": "This is Q2 ans2",
"ans3": "This is Q2 ans3",
"ans4": "This is Q2 ans4",
"correctans": "ans2",
"notes": "This is Q2 note 1",
"category": "5",
"flag": "ans2"
},
{
"quesnum": 3,
"questype": 6,
"question": "gkjhgjkgkg",
"ans1": "ghdfhdghfd",
"ans2": "Tuituiyt",
"ans3": "Tiuytui9",
"ans4": "Tauitui.247",
"correctans": "ans2",
"notes": "Article iutuC",
"category": "5",
"flag": "ans2"
},
{
"quesnum": 7,
"questype": 2,
"question": " how many",
"ans1": "isi",
"ans2": "thiis\\r\\n",
"ans3": "yes",
"ans4": "no",
"correctans": "yes",
"notes": "refer back to yes",
"category": "0",
"flag": "yes"
},
{
"quesnum": 8,
"questype": 2,
"question": " how many",
"ans1": "isi",
"ans2": "thiis",
"ans3": "yes",
"ans4": "no",
"correctans": "yes",
"notes": "refer back to yes",
"category": "0",
"flag": "yes"
},
{
"quesnum": 9,
"questype": 2,
"question": "How many apples can I eat in one day?",
"ans1": "42 apples",
"ans2": "6 apples",
"ans3": "89 apples",
"ans4": " 42 oranges",
"correctans": "ans2",
"notes": "try eating apples",
"category": "8",
"flag": "ans2"
},
{
"quesnum": 10,
"questype": 2,
"question": " how many",
"ans1": "isi",
"ans2": "thiis\\r\\n\\r\\n\\r\\n",
"ans3": "yes",
"ans4": "no",
"correctans": "yes",
"notes": "refer back to yes",
"category": "0",
"flag": "yes"
}
]
}
这是我希望的数组结构:
allInfo {
innerList {
JSONObject
JSONObject
JSONObject
}
innerList {
JSONObject
JSONObject
JSONObject
}
innerList {
JSONObject
JSONObject
JSONObject
}
innerList {
JSONObject
JSONObject
JSONObject
}
innerList {
JSONObject
JSONObject
JSONObject
}
innerList {
JSONObject
JSONObject
JSONObject
}
}
但相反,这就是我最终的结果(我认为):
allInfo {
innerList {
JSONObject
JSONObject
JSONObject
JSONObject
JSONObject
JSONObject
JSONObject
JSONObject
JSONObject
JSONObject
JSONObject
JSONObject
JSONObject
JSONObject
JSONObject
JSONObject
JSONObject
JSONObject
}
}
它应该在一个列表中创建多个列表,以便我可以调用对它的访问并在 TextView 中设置文本,如下所示:
textView.setText(allInfo.get(2).get(4)); //example
解决方案
我猜如果你把这条线List<String> innerList = new ArrayList();
放在for循环中,它每次都会正确初始化innerList。
推荐阅读
- java - 具有自己的数据库用户详细信息的云端点的自定义身份验证
- javascript - 媒体内容的不同路径
- mysql - 按条件用其他列值更新 Sql 列
- javascript - 即使从应用程序切换后,如何确保我添加到列表中的内容仍然存在
- node.js - 使用 Typescript 向 expressjs 中的 req 对象添加属性
- python - numpy linalg.solve,不是方阵
- micronaut - Micronaut 为 MySQL 配置 micronaut-jdbc-tomcat 数据源
- android - ADB 无法连接到 ip 地址
- java - 错误 404--未找到服务器未找到任何与请求 URI 匹配的内容
- javascript - 图片预览后热自动上传图片到服务器