以下JSON響應我得到這樣的JSON響應,如何獲得價值形成的Android
{
"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"
}
]
}
]
}
}
我可以能夠從中得到響應的所有數據的使用下面的代碼,遺憾的是我不能t顯示適合問題的答案。
我的代碼片段,
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();
}
}
我需要表現出這樣的這種反應,我不知道如何來顯示設置問題和答案的格式。
您可以添加您logcat的? – Houcine
@Houcine:你需要數組列表? – Aerrow
我只需要看看你是否正確地檢索jsonArrays :)。 注意:你可以直接使用json.getJsonObject(String key);和json.getJsonArray(String key);而不是獲取字符串和instanciate與該字符串 – Houcine