org.json.JSONException:。JSONObject的[ “值”]不是JSONArray”

org.json.JSONException:。JSONObject的[ “值”]不是JSONArray”

问题描述:

这是我的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。” –

已解决...值不是一个数组,而是一个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); 

        }