博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Json map
阅读量:7069 次
发布时间:2019-06-28

本文共 2103 字,大约阅读时间需要 7 分钟。

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
resultMap = new HashMap
();  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();             }         }

 以上代码实现排除牟些字段, 也可以指定某些字段

        List
cataTempList = cataTemp.find(); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter(){ @Override public boolean apply(Object arg0, String name, Object value) { if(name.equals("id") || name.equals("name")){ return false; }else{ return true; } }}); jsonConfig.setIgnoreDefaultExcludes(true); jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); JSONArray jsonArray = JSONArray.fromObject(cataTempList, jsonConfig); resultMap.put("success", true); resultMap.put("msg", jsonArray); resultMap.put("errmsg", "");

 

转载于:https://www.cnblogs.com/rocky-fang/p/5908142.html

你可能感兴趣的文章
iOS 证书转换相关命令
查看>>
开发者论坛一周精粹(第十二期):如何通过快照的瘦身和删除来节省储存费用...
查看>>
LeetCode之二叉树问题
查看>>
Vue.js 源码学习笔记
查看>>
社会化分享如何影响你的APP
查看>>
FALCON: An Optimizations Java JIT
查看>>
centos下PIL的安装
查看>>
IOST节点计划全面升级: 全球寻找1000位IOST合伙人
查看>>
NSNotificationCenter
查看>>
版本号正则表达式匹配
查看>>
新手入门mysql(一)
查看>>
nodejs实现微信小程序支付功能及相关问题总结
查看>>
Django web框架-win10搭建django2.1.7开发环境,定义简易视图及网址
查看>>
HTTP各种常见状态码
查看>>
Git相关小记
查看>>
Python将字符串转为字典最佳实践
查看>>
Python3之字符串格式化format函数详解(下)
查看>>
Python笔记001-类的特殊方法
查看>>
10分钟快速精通rollup.js——Vue.js源码打包原理深度分析
查看>>
蜂鸟智游大数据:“人在囧途”的春运,航空公司们可操碎了心
查看>>