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