星空网 > 软件开发 > Java

Json map

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

 




原标题:Json map

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流