[英]Getting nested JSON array and objects and facing Json Exception
这是我们从服务器端获得的嵌套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\"}]]"
这是获得此代码的代码,但是在执行编译器时会捕获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响应之前删除转义序列。 在解析响应之前执行此操作
response.toString().replaceAll("\\\\","");
arr= new JSONArray(response);
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.