简体   繁体   English

解析JSON数组时,获取异常“ org.json.JSONObject类型的答案无法转换为JSONArray”

[英]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.

相关问题 org.json.JSONObject无法转换为JSONArray - org.json.JSONObject cannot be converted to JSONArray 无法将org.json.JSONObject类型的数据转换为JSONArray - Data of type org.json.JSONObject cannot be converted to JSONArray 类型为org.json.JSONObject的响应无法转换为JSONArray - at response of type org.json.JSONObject cannot be converted to JSONArray at 类型 org.json.JSONObject 的数据无法转换为 JSONArray - at data of type org.json.JSONObject cannot be converted to JSONArray 值类型 org.json.JSONObject 无法转换为 JSONArray - Value type org.json.JSONObject cannot be converted to JSONArray 错误org.json.JSONObject无法转换为JSONArray - error org.json.JSONObject cannot be converted to JSONArray 在Android中解析JSON响应“无法将org.json.JSONObject类型的响应转换为JSONArray” - Parse JSON response in android “response of type org.json.JSONObject cannot be converted to JSONArray” 我有一个错误:类型 org.json.JSONObject 无法转换为 JSONArray - I have an error : type org.json.JSONObject cannot be converted to JSONArray org.json.JSONException:值 {“storeid0”:[“1535”],“storeid1”:[“1862”]} 类型为 org.json.JSONObject 的 idddsss 无法转换为 JSONArray - org.json.JSONException: Value {“storeid0”:[“1535”],“storeid1”:[“1862”]} at idddsss of type org.json.JSONObject cannot be converted to JSONArray 获取错误:解析数据 org.json.JSONException 时出错:无法将 org.json.JSONArray 类型的值转换为 JSONObject - getting error :Error parsing data org.json.JSONException: Value of type org.json.JSONArray cannot be converted to JSONObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM