如何解析嵌套的JSON数组?

时间:2017-02-23 10:09:49

标签: android arrays json

在我的应用程序中,我使用php web服务来获取json数据。 下面是从Web服务获取的json数据:

    {  
   "question_data":[  
      {  
         "section":{  
            "section_id":"1",
            "section_name":"\u0935\u0938\u094d\u0924\u0941\u0928\u093f\u0937\u094d\u0920 \u092a\u094d\u0930\u0936\u094d\u0928",
            "no_of_que":"25",
            "duration":"25"
         },
         "question_list":[  
            {  
               "question":{  
                  "reference_id":"1",
                  "reference_name":"Fundamental",
                  "reference_desc":"",
                  "reference_image":null,
                  "created_by":null,
                  "created_on":null,
                  "modified_by":null,
                  "modified_on":null,
                  "display":"Y",
                  "question_id":"407",
                  "section_id":"1",
                  "question":"Flat blades are mainly used in \u2026\u2026.. Type.\t\r\n",
                  "question_mar":"\u2026\u2026\u2026 \u092a\u094d\u0930\u093f\u0902\u091f\u0930 \u092e\u0927\u094d\u092f\u0947 \u092e\u0941\u0916\u094d\u092f\u0924\u094d\u0935\u0947 \u092b\u094d\u0932\u0945\u091f \u092c\u0947\u0921 \u0909\u092a\u092f\u094b\u0917 \u0915\u0947\u0932\u0947\u0932\u093e \u0905\u0938\u0924\u094b.\t\t\t\r\n",
                  "question_hindi":"\r\n\t\r\n\t\t\r\n\t\t\t 
……… \u092a\u094d\u0930\u093f\u0902\u091f\u0930 \u092e\u0927\u094d\u092f\u0947 \u092e\u0941\u0916\u094d\u092f\u0924\u094d\u0935\u0947 \u092b\u094d\u0932\u0945\u091f \u092c\u0947\u0921 \u0909\u092a\u092f\u094b\u0917 \u0915\u0947\u0932\u0947\u0932\u093e \u0905\u0938\u0924\u094b.<\/td>\r\n\t\t<\/tr>\r\n\t<\/tbody>\r\n<\/table>\r\n",
                  "question_image":null,
                  "option_id":"1498",
                  "exam_type":"main"
               },
               "option":[  
                  {  
                     "option_id":"1496",
                     "question_id":"407",
                     "option":"Xerox \t",
                     "option_image":null,
                     "display":"Y"
                  },
                  {  
                     "option_id":"1497",
                     "question_id":"407",
                     "option":"photo\t",
                     "option_image":null,
                     "display":"Y"
                  },
                  {  
                     "option_id":"1498",
                     "question_id":"407",
                     "option":"Plotter",
                     "option_image":null,
                     "display":"Y"
                  },
                  {  
                     "option_id":"1499",
                     "question_id":"407",
                     "option":"DeskJet",
                     "option_image":null,
                     "display":"Y"
                  }
               ]
            },
            {  
               "question":{  
                  "reference_id":"1",
                  "reference_name":"Fundamental",
                  "reference_desc":"",
                  "reference_image":null,
                  "created_by":null,
                  "created_on":null,
                  "modified_by":null,
                  "modified_on":null,
                  "display":"Y",
                  "question_id":"420",
                  "section_id":"1",
                  "question":"For protection of computer from virus \u2026\u2026\u2026.. Program should be used.\t\r\n",
                  "question_mar":"\u0915\u0949\u092e\u094d\u092a\u094d\u092f\u0941\u091f\u0930 \u0935\u093e\u092f\u0930\u0938 \u092a\u093e\u0938\u0942\u0928 \u0906\u092a\u0932\u094d\u092f\u093e \u0915\u0949\u092e\u094d\u092a\u094d\u092f\u0941\u091f\u0930\u091a\u093e \u092c\u091a\u093e\u0935 \u0915\u0930\u0923\u094d\u092f\u093e\u0938\u093e\u0920\u0940 \u0906\u092a\u0932\u094d\u092f\u093e \u0915\u0949\u092e\u094d\u092a\u094d\u092f\u0941\u091f\u0930\u0935\u0930 \u2026\u2026.. \u092a\u094d\u0930\u094b\u0917\u0945\u092e \u0972\u0915\u094d\u091f\u093f\u0935\u0947\u091f \u0915\u0930\u0942\u0928 \u0920\u0947\u0935\u093e\u0935\u093e.\t\t\t\r\n",
                  "question_hindi":" 
\u0915\u0949\u092e\u094d\u092a\u094d\u092f\u0941\u091f\u0930 \u0935\u093e\u092f\u0930\u0938 \u092a\u093e\u0938\u0942\u0928 \u0906\u092a\u0932\u094d\u092f\u093e \u0915\u0949\u092e\u094d\u092a\u094d\u092f\u0941\u091f\u0930\u091a\u093e \u092c\u091a\u093e\u0935 \u0915\u0930\u0923\u094d\u092f\u093e\u0938\u093e\u0920\u0940 \u0906\u092a\u0932\u094d\u092f\u093e \u0915\u0949\u092e\u094d\u092a\u094d\u092f\u0941\u091f\u0930\u0935\u0930 …….. \u092a\u094d\u0930\u094b\u0917\u0945\u092e \u0972\u0915\u094d\u091f\u093f\u0935\u0947\u091f \u0915\u0930\u0942\u0928 \u0920\u0947\u0935\u093e\u0935\u093e.           <\/p>\r\n",
                  "question_image":null,
                  "option_id":"1548",
                  "exam_type":"main"
               },
               "option":[  
                  {  
                     "option_id":"1548",
                     "question_id":"420",
                     "option":"Anti Virus\t",
                     "option_image":null,
                     "display":"Y"
                  },
                  {  
                     "option_id":"1549",
                     "question_id":"420",
                     "option":"Word\t",
                     "option_image":null,
                     "display":"Y"
                  },
                  {  
                     "option_id":"1550",
                     "question_id":"420",
                     "option":"Excel\t",
                     "option_image":null,
                     "display":"Y"
                  },
                  {  
                     "option_id":"1551",
                     "question_id":"420",
                     "option":"Notepad",
                     "option_image":null,
                     "display":"Y"
                  }
               ]
            }...........

现在我想只得到&#39; question_id&#39; ,&#39;问题&#39; ,&#39; question_mar&#39; ,&#39; question_hindi&#39;和&#39;选项&#39; ,&#39; option_id&#39;来自各自的json数组。

以下是我获取上述数据的代码:

JSONObject mainObj = new JSONObject(sb.toString());
            JSONArray question_data = mainObj.getJSONArray("question_data");
            for (int i = 0; i < question_data.length(); i++) {
                JSONObject elem = question_data.getJSONObject(i);

                JSONArray questions = elem.getJSONArray("question_list");
                for (int k = 0; k < questions.length(); k++) {
                    JSONObject queandopt = questions.getJSONObject(k);

                    JSONArray question_list = queandopt.getJSONArray("question");
                    for (int j = 0; j < question_list.length(); j++) {
                        JSONObject question = question_list.getJSONObject(j);

                        String question_id = question.getString("question_id");
                        String question_eng = question.getString("question");
                        String question_mar = question.getString("question_mar");
                        String question_hindi = question.getString("question_hindi");
                    }

                    JSONArray option_list = queandopt.getJSONArray("option");
                    for (int j = 0; j < option_list.length(); j++) {
                        JSONObject option = option_list.getJSONObject(j);

                        String option_id = option.getString("option_id");
                        String option_desc = option.getString("option");
                    }
                }
            }

我在获取异常时遇到异常&#39; org.json.JSONException&#39; 如何解决问题??

请帮助!!

1 个答案:

答案 0 :(得分:1)

试试这个,

 try {
            JSONObject mainObj = new JSONObject(sb.toString());
            JSONArray question_data = null;

                question_data = mainObj.getJSONArray("question_data");

            for (int i = 0; i < question_data.length(); i++) {
                JSONObject elem = question_data.getJSONObject(i);

                JSONArray questions = elem.getJSONArray("question_list");
                for (int k = 0; k < questions.length(); k++) {
                    JSONObject queandopt = questions.getJSONObject(k);

                    JSONObject question_obj=queandopt.getJSONObject("question");

                    String question_id_value = question_obj.getString("question_id");
                    String question_value = question_obj.getString("question");
                    String question_mar_value = question_obj.getString("question_mar");
                    String question_hindi_value = question_obj.getString("question_hindi");

                    Log.d("TAG","question_id_value"+question_id_value+"question_value"+question_value+"question_mar_value"+question_mar_value+"question_hindi_value"+question_hindi_value);

                    JSONArray option_list = queandopt.getJSONArray("option");
                    for (int j = 0; j < option_list.length(); j++) {
                        JSONObject option = option_list.getJSONObject(j);

                        String option_id = option.getString("option_id");
                        String option_desc = option.getString("option");
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }