Я пытаюсь создать игру-викторину для удовольствия, но я пытаюсь понять, как извлечь нужную строку из JSONArray в Android Studio
Я получаю журнал «JSON» и «результаты» появляются в Logcat, но я не могу понять, как установить переменную mQuestion в соответствующую строку.
JSON
{
"response_code":0,
"results":[{
"category":"General Knowledge",
"type":"multiple",
"difficulty":"medium",
"question":"According to the BBPA, what is the most common pub name in the UK?",
"correct_answer":"Red Lion",
"incorrect_answers": [
"Royal Oak",
"White Hart",
"King's Head"
]
}]
}
Мой код
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.i("JSON", s);
try {
JSONObject jsonObject = new JSONObject(s);
String results = jsonObject.getString("results");
Log.i("results", results);
JSONArray arr = new JSONArray(results);
for (int i = 0; i < arr.length(); i++) {
mQuestion = arr.getJSONObject(3).getString("question");
Log.i("Question", mQuestion);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Я получаю в Logcat W / System.err: at ... MainActivity $ getQuestion.onPostExecute (MainActivity.java:67)
67 - строка, содержащая "mQuestion = arr.getJSONObject (3) .getString (" question ");"