2017-06-13 116 views
0

這是我的JSON數組org.json.JSONException:。JSONObject的[ 「值」]不是JSONArray」

[ 
    { 
    "type":"select", 
    "label":"Select", 
    "className":"form-control", 
    "name":"select-1497355331262", 
    "values":[ 
       { 
       "label":"Option 1", 
       "value":"option-1", 
       "selected":true 
       }, 
      { 
       "label":"Option 2", 
       "value":"option-2" 
      }, 
      { 
       "label":"Option 3", 
       "value":"option-3" 
      } 
     ] 
     } 
] 

我Appraoch:

JSONArray js = null; 
      try { 
       js = new JSONArray(json); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      JSONObject obj = null; 

      for (int i = 0; i < js.length(); i++) { 

       try { 
        obj = js.getJSONObject(i); 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

       try { 
        type = obj.getString("type"); 
       } catch (JSONException e) { 
        type = null; 
       } 

       System.out.println("Type : " + type); 


       try { 
        JSONArray jContent = obj.getJSONArray("values"); 
        System.out.println(jContent.toString()); 
        for (int iterate = 0; iterate < jContent.length(); iterate++) { 
         JSONObject inner = jContent.getJSONObject(iterate); 
         String inner_label = (String) inner.get("label"); 

         System.out.println(inner_label); 

         String val = (String) inner.get("value"); 
         boolean sel; 
         try { 
          sel = (boolean) inner.get("selected"); 

         } catch (JSONException j) { 
          sel = false; 
         } 
         System.out.print(inner_label); 
         System.out.print(" " + val); 
         System.out.println(" " + sel); 

        } 

       } catch (JSONException e) { 
        System.out.println("There is some error"); 
       } 

       try { 
        label = obj.getString("label"); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
       try { 
        access = obj.getBoolean("access"); 
       } catch (JSONException e) { 
        access = false; 
       } 

       try { 
        role = obj.getString("role"); 
       } catch (JSONException e) { 
        role = null; 
       } 

       try { 
        subtype = obj.getString("subtype"); 
       } catch (JSONException e) { 

        subtype = null; 
       } 

       try { 
        maxlength = obj.getString("maxlength"); 
       } catch (JSONException e) { 

        maxlength = null; 
       } 

       try { 
        name = obj.getString("name"); 
       } catch (JSONException e) { 

        name = null; 
       } 

       try { 
        description = obj.getString("description"); 
       } catch (JSONException e) { 

        description = null; 
       } 

       try { 
        classname = obj.getString("className"); 
       } catch (JSONException e) { 

        classname = null; 
       } 

       try { 
        placeholder = obj.getString("placeholder"); 
       } catch (JSONException e) { 

        placeholder = null; 
       } 

       try { 
        value = obj.getString("value"); 
       } catch (JSONException e) { 

        value = null; 
       } 

       try { 
        required = obj.getBoolean("required"); 
       } catch (JSONException e) { 

        required = false; 
       } 
       try { 
        toggle = obj.getBoolean("toggle"); 
       } catch (JSONException e) { 
        toggle = false; 
       } 
       try { 
        inline = obj.getBoolean("inline"); 
       } catch (JSONException e) { 
        inline = false; 
       } 
       try { 
        enableother = obj.getBoolean("other"); 
       } catch (JSONException e) { 
        enableother = false; 
       } 
       try { 
        multipleFiles = obj.getBoolean("multiple"); 
       } catch (JSONException e) { 
        multipleFiles = false; 
       } 
       try { 
        style = obj.getString("style"); 
       } catch (JSONException e) { 
        style = null; 
       } 
       try { 
        min = obj.getString("min"); 
       } catch (JSONException e) { 
        min = null; 
       } 
       try { 
        max = obj.getString("max"); 
       } catch (JSONException e) { 
        max = null; 
       } 
       try { 
        step = obj.getString("step"); 
       } catch (JSONException e) { 
        step = null; 
       } 
       try { 
        rows = obj.getString("rows"); 
       } catch (JSONException e) { 
        rows = null; 
       } 

我能夠得到所有其他對象值我無法讀取值數組我已經寫了它的代碼,但它直接去catch塊打印錯誤消息「有一些錯誤」。請幫助。

+2

爲什麼你的catch塊如此薄弱?爲什麼不打印「有一些錯誤」,爲什麼不像你在其他地方打印堆棧跟蹤一樣,'e.printStackTrace();'?這會給你更多有用的錯誤信息。 –

+1

另外,爲什麼這麼多小的try/catch塊? –

+0

@HovercraftFullOfEels嘿檢查錯誤「org.json.JSONException:JSONObject [」values「]不是JSONArray。」 –

回答

0

已解決...值不是一個數組,而是一個o bject ....所以這就是我所做的。有效。

try { 

       String j2 = obj.getString("values"); 
       JSONArray jContent = new JSONArray(j2); 
            for (int iterate = 0; iterate < jContent.length(); iterate++) { 
         JSONObject inner = jContent.getJSONObject(iterate); 
         String inner_label = (String) inner.get("label"); 

         System.out.println(inner_label); 

         String val = (String) inner.get("value"); 
         boolean sel; 
         try { 
          sel = (boolean) inner.get("selected"); 

         } catch (JSONException j) { 
          sel = false; 
         } 
         System.out.print(inner_label); 
         System.out.print(" " + val); 
         System.out.println(" " + sel); 

        } 
相關問題