【问题标题】:Getting nested JSON array and objects and facing Json Exception获取嵌套的 JSON 数组和对象并面临 Json 异常
【发布时间】:2015-05-20 13:22:32
【问题描述】:

这是我们从服务器端获得的嵌套 Json 数组。

回应:

"[[{\"userID\":\"1\",\"roleID\":\"1\",\"userName\":\"GHG Admin\",\"email\":\"vikas.rana@redalkemi.in\",\"password\":\"b2314cd68cdfb7545dacd0b4ecf6a190\",\"userImage\":\"image12.jpg\",\"dtAdded\":\"2013-05-28\",\"dtUpdated\":\"2013-05-28\",\"enumStatus\":\"A\"}],[{\"settingID\":\"1\",\"moduleName\":\"event\",\"displayName\":\"Ev\\\"ent's1\",\"enumStatus\":\"E\"},{\"settingID\":\"2\",\"moduleName\":\"notice\",\"displayName\":\"Not\\\"ic'es\",\"enumStatus\":\"E\"},{\"settingID\":\"3\",\"moduleName\":\"quote\",\"displayName\":\"Qu\\\"ot'es\",\"enumStatus\":\"E\"},{\"settingID\":\"6\",\"moduleName\":\"crawlingtext\",\"displayName\":\"Crawling Text\",\"enumStatus\":\"D\"}]]"

这里是获取这个的代码,但是在执行编译器时会进入 catch JSON 异常。

JSONArray arr;
try {
            if (response != null) {

        System.out.println(response);

                                arr= new JSONArray(response);
                                JSONArray a1= arr.getJSONArray(0);
                                JSONObject  obj1= a1.getJSONObject(0);
                                    map = new HashMap<String, String>();

                                    // Retrive JSON Objects
                                    map.put("userID", obj1.getString("userID"));
                                    map.put("userName", obj1.getString("userName"));


                                    data = new ArrayList<HashMap<String, String>>();
                                    // Set the JSON Objects into the array
                                    data.add(map);
                                    id = (String) map.get("userID");
                                    ID=Integer.parseInt(id);
                                    name = (String) map.get("userName");
                                    System.out.println(id+name);

                                JSONArray a2=arr.getJSONArray(1);   
                                for (int i = 0; i < a2.length(); i++) {

                                JSONObject obj2= a2.getJSONObject(i);
                                map = new HashMap<String, String>();
                                map.put("settingID", obj2.getString("settingID"));
                                map.put("moduleName", obj2.getString("moduleName"));
                                data = new ArrayList<HashMap<String, String>>();
                                // Set the JSON Objects into the array
                                data.add(map);
                                String Module=(String)map.get("moduleName");
                                        System.out.println("MODULE="+Module);

                    }}
                    }



                            catch ( JSONException e ) {
                                System.out.println("JSON Error parsing JSON");
                            }

【问题讨论】:

  • 缩进您的 JSON 数据以及您的代码以获得清晰的内容。
  • 通常 JSONException 是不言自明的。你的例外说明了什么?
  • jsonformatter.curiousconcept.com 中试试你的json,你会很清楚你的json 是如何格式化的
  • 编译器不执行循环或尝试代码,但只显示异常消息
  • @AnshulTyagi 感谢您的帮助,PHP 开发人员更正了格式。

标签: android json getjson


【解决方案1】:

在解析 JSON 响应之前,您似乎没有从 JSON 响应中删除转义序列。在解析响应之前执行此操作

response.toString().replaceAll("\\\\","");
arr= new JSONArray(response);
....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 2019-08-11
    相关资源
    最近更新 更多