zoukankan      html  css  js  c++  java
  • java把结果集序列化成json通过out流传给前台步骤

    1.把处理好的list或map序列化成JSON字符

    /**
         * 序列化集合成JSON字符
         * @param list
         * @return
         */
        public static String structureConfigParamsGroupJSONData(List<?> list)
        {
            JSONSerializer serializer = new JSONSerializer();
            String json="";
            json = serializer.exclude("*.class").deepSerialize(list).replaceAll(":\s*null\s*", ":""");
            return json;
        }
        
        public static String structureConfigParamsGroupJSONData(Map<String, ?> map)
        {
            JSONSerializer serializer = new JSONSerializer();
            String json="";
            json = serializer.exclude("*.class").deepSerialize(map).replaceAll(":\s*null\s*", ":""");
            return json;
        }
    
    
    作者:littleDragon
    链接:https://www.jianshu.com/p/c4068c941939
    來源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    2.输出JSON

    /**
         * 输出JSON
         * 
         * @param response
         * @param result
         * @throws IOException
         */
        public void print(HttpServletResponse response, String result) throws IOException
        {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/json;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print(result);
            out.flush();
            out.close();
         }
    
    
    作者:littleDragon
    链接:https://www.jianshu.com/p/c4068c941939
    來源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

     另外,Gson 是google解析Json的一个开源框架,同类的框架fastJson,JackJson等等,也很好用。请自行百度,有很多参考文章。

  • 相关阅读:
    守护进程、互斥锁、生产者消费者模型
    实现并发编程的基础理论
    udp协议
    进程与进程池
    tcp协议产生-粘包问题的解决方案
    day21面向对象_类
    day16_面向过程编程与模块导入
    day15_函数递归_匿名函数_内置函数
    三、运算符(阶段二)
    二、(续)基础语法--常量、变量和注释(阶段二)
  • 原文地址:https://www.cnblogs.com/lijingran/p/8385624.html
Copyright © 2011-2022 走看看