好的,因此我需要使用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
                    版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
我猜如果您将行List innerList = new ArrayList();放置在for循环中,它将每次正确地初始化innerList。