这是我的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": "The full load current for a three phase motor is found in _____.",
"ans1": "Table 430.248",
"ans2": "Table 430.250",
"ans3": "Table 430.249",
"ans4": "Table 430.247",
"correctans": "ans2",
"notes": "Article 430 2017 NEC",
"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"
}
]
}
这是我用于解析JSONArray及其结构输出的for循环。
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 innerList;
输出:
allInfo {
innerList {
JSONObject
JSONObject
JSONObject //now just imagine 67 more of these per every innerList
}
innerList {
JSONObject
JSONObject
JSONObject
}
innerList {
JSONObject
JSONObject
JSONObject
}
innerList {
JSONObject
JSONObject
JSONObject
}
innerList {
JSONObject
JSONObject
JSONObject
}
innerList {
JSONObject
JSONObject
JSONObject
}
innerList {
JSONObject
JSONObject
JSONObject
}
}
所发生的是,与其采用数组中的前10个元素并将其放入自己的innerList中,不如将所有70个元素都放入每个innerList中。每个innerList总共只能包含10个元素。因此,这意味着如果在所有7个innerList中都解决此问题,则总共将有70个问题。
那我该怎么做才能只解析JSONArray的第一部分?有没有我忘记的方法?