Json map,jsonmap
Json map,jsonmap 1. 返回数据形式 Class returnMsg{ boolean success; String
Json map,jsonmap
1. 返回数据形式
Class returnMsg{
boolean success;
String msg;
String errorMsg;
}
2.问题
当msg中的数据由对象 或 集合转换而来时, 用JSONObject.fromObject(obj).toString()返回后带有'\'
3. 解决方案
将集合类型数据转换成jsonArray,用Map来存放数据,返回map
userList = accountManager.findDeptUser(deptId);JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes(new String[]{"role"}); jsonConfig.setIgnoreDefaultExcludes(true); jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
JSONArray jsonArray = JSONArray.fromObject(userList, jsonConfig);
Map<String, Object> resultMap = new HashMap<String, Object>(); resultMap.put("success", true); resultMap.put("msg", jsonArray); resultMap.put("errmsg", "");
response.setHeader("Cache-Control", "no-cache");
response.setContentType("text/json; charset=UTF-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = null;
try {
out = response.getWriter();
out.print(JSONArray.fromObject(resultMap));
out.flush();
} catch (Exception e) {
}finally{
if(out!=null){
out.close();
}
}