私はこのようなJsonの応答を受け取ります、
{
"data": {
"id": "1",
"name": "General Knowlege Questions",
"description": "This questions will test your General ",
"questions": [
{
"id": "1",
"text": "Who invented the BALLPOINT PEN?",
"type": "2",
"answers": [
{
"id": "1",
"text": "Biro Brothers"
},
{
"id": "2",
"text": "Waterman Brothers"
},
{
"id": "3",
"text": "Bicc Brothers"
},
{
"id": "4",
"text": "Write Brothers "
}
]
},
{
"id": "2",
"text": "What J. B. Dunlop invented?",
"type": "2",
"answers": [
{
"id": "5",
"text": "Pneumatic rubber tire"
},
{
"id": "6",
"text": "Automobile wheel rim"
},
{
"id": "7",
"text": "Rubber boot"
},
{
"id": "8",
"text": "Model airplanes"
}
]
},
{
"id": "3",
"text": "Which scientist discovered the radioactive element radium?",
"type": "2",
"answers": [
{
"id": "9",
"text": "Isaac Newton"
},
{
"id": "10",
"text": "Albert Einstein"
},
{
"id": "11",
"text": "Benjamin Franklin"
},
{
"id": "12",
"text": "Marie Curie"
}
]
},
{
"id": "4",
"text": "What now-ubiquitous device was invented by Zenith engineer Eugene Polley in 1955?",
"type": "1",
"answers": [
{
"id": "13",
"text": "Microwave oven"
},
{
"id": "14",
"text": "Remote control"
}
]
},
{
"id": "5",
"text": "What Benjamin Franklin invented?",
"type": "1",
"answers": [
{
"id": "15",
"text": "Bifocal spectacles"
},
{
"id": "16",
"text": "Radio"
}
]
}
]
}
}
次のコードを使用して、この応答からすべてのデータを取得できますが、残念ながら、適切な質問の回答セットを表示できません。
私のコードスニペットは、
public void getQuestions(String survey_response) {
try {
JSONObject first_obj = new JSONObject(survey_response);
String data_stirng = first_obj.getString("data");
JSONObject sub_obj = new JSONObject(data_stirng);
String name_val = sub_obj.getString("questions");
JSONArray questions_array = new JSONArray(name_val);
for (int i = 0; i < questions_array.length(); i++) {
JSONObject qus_elements = questions_array.getJSONObject(i);
QUESTION_ID = qus_elements.getString("id");
QUESTION_TEXT = qus_elements.getString("text");
QUESTION_TYPE = qus_elements.getString("type");
String answers_val = qus_elements.getString("answers");
JSONArray ans_array = new JSONArray(answers_val);
Log.v("Answers Array Values", ans_array + "");
for (int j = 0; j < ans_array.length(); j++) {
JSONObject ans_elements = ans_array.getJSONObject(j);
ANSWERS_ID = ans_elements.getString("id");
ANSWERS_TEXT = ans_elements.getString("text");
answers_id.add(ANSWERS_ID);
answers_text.add(ANSWERS_TEXT);
}
ques_id.add(QUESTION_ID);
ques_text.add(QUESTION_TEXT);
ques_type.add(QUESTION_TYPE);
}
// Log.v("QUESTION ID ARRAY", ques_id + "");
// Log.v("QUESTION Text ARRAY", ques_text + "");
// Log.v("QUESTION type ARRAY", ques_type + "");
Log.v("ANSWERS ID ARRAY", answers_id + "");
Log.v("ANSWERS TEXT ARRAY", answers_text + "");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
この応答をこのように表示する必要があります。一連の質問と回答をこの形式で表示する方法がわかりません。