org.json.JSONException:JSONObject ["values"]不是JSONArray. [英] org.json.JSONException: JSONObject["values"] is not a JSONArray."

查看:147
本文介绍了org.json.JSONException:JSONObject ["values"]不是JSONArray.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的json数组.

This my json array.

[
  {
    "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"
            }
         ]
       }
]

我的眼神:

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

我能够获取所有其他对象的值,但我无法读取values数组.我已经为它编写了代码,但它直接转到catch块,并显示错误消息有一些错误".谢谢您的帮助.

I am able to get all other objects values I am unable to read the values array. I have written the code for it but its directly going to the catch block printing the error message "There is some error". Thnaks for the help.

推荐答案

已解决...值不是一个数组,而是一个对象....所以这就是我所做的.奏效了.

Solved...Values was not an array it was an object....So here's what I did. It worked.

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

                    }

这篇关于org.json.JSONException:JSONObject ["values"]不是JSONArray.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆