[英]Getting exception “ at answer of type org.json.JSONObject cannot be converted to JSONArray” while parsing JSON array
I need to segregate question and their answer from the [answer] array in JSON Array "subject_wise_test_ans". 我需要从JSON数组“ subject_wise_test_ans”中的[answer]数组中隔离问题及其答案。
JSONArray temp = response.getJSONArray("subject_wise_test_ans");
JSONObject buffer1;
for (int i = 0; i < temp.length(); i++) {
JSONObject buffer = temp.getJSONObject(i);
String id= buffer.getString("id");
String test_id= buffer.getString("test_id");
String subject_name = buffer.getString("subject_name");
JSONArray answer = buffer.getJSONArray("answer");
String marks_pluse = buffer.getString("marks_pluse");
String marks_minus = buffer.getString("marks_minus");
String answer_set = buffer.getString("answer");
String answer_key = buffer.getString("answer_key");
Log.i("Answer Array 1", answer.toString());
This is JSON output: 这是JSON输出:
{
"subject_wise_test_ans":[
{
"id":52,
"test_id":"7",
"subject_name":"8",
"question_no":"25",
"answer":{
"1":"B",
"2":"A",
"3":"B",
"4":"D",
"5":"A",
"6":"D",
"7":"B",
"8":"D",
"9":"A",
"10":"A",
"11":"C",
"12":"C",
"13":"D",
"14":"D",
"15":"D",
"16":"D",
"17":"B",
"18":"B",
"19":"B",
"20":"D",
"21":"A",
"22":"A",
"23":"B",
"24":"D",
"25":"B"
},
"marks_pluse":"2",
"marks_minus":"0.67",
"answer_key":"c6840363cdcaca8ca87cc8755f76898c.pdf"
},
...
I'm getting exception: 我遇到异常:
W/System.err: org.json.JSONException: Value {"1":"B","2":"A","3":"B","4":"D","5":"A","6":"D","7":"B","8":"D","9":"A","10":"A","11":"C","12":"C","13":"D","14":"D","15":"D","16":"D","17":"B","18":"B","19":"B","20":"D","21":"A","22":"A","23":"B","24":"D","25":"B"}
at answer of type org.json.JSONObject cannot be converted to JSONArray
Replace JSONArray answer = buffer.getJSONArray("answer");
替换
JSONArray answer = buffer.getJSONArray("answer");
by 通过
JSONObject answer = buffer.getJSONObject("answer");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.