首页 > Web开发 > 详细

Json map

时间:2016-09-26 10:49:45      阅读:183      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/rocky-fang/p/5908142.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!