2017-02-23 234 views
-2

在我的應用程序中,我使用php web服務來獲取json數據。 下面是一個被取出從Web服務的JSON數據:如何解析嵌套的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" 
        } 
       ] 
      }........... 

現在,我想只有「question_id」,「問題」,「question_mar」,「question_hindi」和「選項」,「option_id」相應的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"); 
        } 
       } 
      } 

我得到異常而取此爲「org.json.JSONException」 如何解決這個問題?

請幫忙!!

+2

錯誤是這裏.. JSONArray question_list = queandopt.getJSONArray( 「問題」); 「問題」是JsonObject不是Json數組 – Piyush

+1

[JSON解析android]可能重複(http://stackoverflow.com/questions/16882037/json-parsing-android) –

+0

發佈logcat異常.. – rafsanahmad007

回答

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(); 
     }