如何使用 Android Volley 解析 JSON 对象中的嵌套数组
How can I parse nested array inside JSON object using Android Volley
我想解析下面的JSON格式,
有人可以建议一种方法吗?谢谢
JSON格式,我要解析
{
"questions": [
{
"question": "What is the scientific name of a butterfly?",
"answers": [
"Apis",
"Coleoptera",
"Formicidae",
"Rhopalocera"
],
"correctIndex": 3
},
{
"question": "How hot is the surface of the sun?",
"answers": [
"1,233 K",
"5,778 K",
"12,130 K",
"101,300 K"
],
"correctIndex": 1
},
{
"question": "Who are the actors in The Internship?",
"answers": [
"Ben Stiller, Jonah Hill",
"Courteney Cox, Matt LeBlanc",
"Kaley Cuoco, Jim Parsons",
"Vince Vaughn, Owen Wilson"
],
"correctIndex": 3
}
]
}
我正在尝试通过以下代码完成它,它没有完成,我可以解析其中的第一个数组和对象。
public 列表 getGeneralQuestion(最终 AnswerListAsyncResponse 回调){
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
URL, null, response -> {
try {
JSONArray jsonArray = response.getJSONArray("questions");
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String question = jsonObject.getString("question");
int correctAnswer = jsonObject.getInt("correctIndex");
GeneralQuestions generalQuestions = new GeneralQuestions(question, correctAnswer);
generalQuestionsArrayList.add(generalQuestions);
JSONArray jsonAnsArray = jsonObject.getJSONArray("answers");
for (int j = 0; j < jsonAnsArray.length(); j++){
JSONArray jsonArrayList = jsonAnsArray.getJSONArray(j);
String answerList = jsonArrayList.getString();///////
GeneralQuestions generalAnswer = new GeneralQuestions(answerList);
generalAnsArrayList.add(generalAnswer);
}
}
我想解析下面的JSON格式, 有人可以建议一种方法吗?谢谢
JSON格式,我要解析
{
"questions": [
{
"question": "What is the scientific name of a butterfly?",
"answers": [
"Apis",
"Coleoptera",
"Formicidae",
"Rhopalocera"
],
"correctIndex": 3
},
{
"question": "How hot is the surface of the sun?",
"answers": [
"1,233 K",
"5,778 K",
"12,130 K",
"101,300 K"
],
"correctIndex": 1
},
{
"question": "Who are the actors in The Internship?",
"answers": [
"Ben Stiller, Jonah Hill",
"Courteney Cox, Matt LeBlanc",
"Kaley Cuoco, Jim Parsons",
"Vince Vaughn, Owen Wilson"
],
"correctIndex": 3
}
]
}
我正在尝试通过以下代码完成它,它没有完成,我可以解析其中的第一个数组和对象。
public 列表 getGeneralQuestion(最终 AnswerListAsyncResponse 回调){
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
URL, null, response -> {
try {
JSONArray jsonArray = response.getJSONArray("questions");
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String question = jsonObject.getString("question");
int correctAnswer = jsonObject.getInt("correctIndex");
GeneralQuestions generalQuestions = new GeneralQuestions(question, correctAnswer);
generalQuestionsArrayList.add(generalQuestions);
JSONArray jsonAnsArray = jsonObject.getJSONArray("answers");
for (int j = 0; j < jsonAnsArray.length(); j++){
JSONArray jsonArrayList = jsonAnsArray.getJSONArray(j);
String answerList = jsonArrayList.getString();///////
GeneralQuestions generalAnswer = new GeneralQuestions(answerList);
generalAnsArrayList.add(generalAnswer);
}
}