开发者社区> 问答> 正文

如何将多个列表放入一个列表?

好的,因此我需要使用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,该列表的类型为allInfo。在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

展开
收起
Puppet 2020-01-12 11:01:10 674 0
1 条回答
写回答
取消 提交回答
  • 我猜如果您将行List innerList = new ArrayList();放置在for循环中,它将每次正确地初始化innerList。

    2020-01-12 11:01:19
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载