获取嵌套的JSON数组和对象并面对Json异常

时间:2015-05-20 04:56:45

标签: android json getjson

这是我们从服务器端获得的嵌套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\"}]]"

以下是获取此代码的代码,但执行complier时会执行catch JSON exeception。

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

1 个答案:

答案 0 :(得分:1)

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

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