zoukankan      html  css  js  c++  java
  • 返回json格式数据乱码

    本文为博主原创,未经允许不得转载:

          原本返回json格式数据的代码:

    @ResponseBody
        @RequestMapping(value = "getListByTime", method = { RequestMethod.POST, RequestMethod.GET })
        public String getListByTime(@RequestParam String aoData,String sEcho,HttpServletRequest request) throws Exception {
    
            AoData paramAoData = new AoData(aoData);
            
            Detail cameraDetail = new Detail ();
            detail.setDisplatStart(paramAoData.getiDisplayStart());
            detail.setDisplayLength(paramAoData.getiDisplayLength());
            List<Detail> list = dataService.getListByTime(detail);
            int size = list.size();
            String[][] data = new String[size][];
            for (int i = 0; i < size; i++) {
                Detail info = list.get(i);
                data[i] = info.values();
    
            }
            int total = dataService.getCameraByTimeTotal(detail);
            
            return JSonUtils.toJSon(new DataTableReturnObject(total, total, sEcho, data));
        }

    以上代码断点从数据库中查询的数据没有乱码,但在前台接收的时候乱码。

    解决方法:

        @ResponseBody
        @RequestMapping(value = "getListByTime", method = { RequestMethod.POST, RequestMethod.GET })
        public void getListByTime(@RequestParam String aoData,String sEcho,HttpServletRequest request) throws Exception {
    
            AoData paramAoData = new AoData(aoData);
            
            Detail cameraDetail = new Detail ();
            detail.setDisplatStart(paramAoData.getiDisplayStart());
            detail.setDisplayLength(paramAoData.getiDisplayLength());
            List<Detail> list = dataService.getListByTime(detail);
            int size = list.size();
            String[][] data = new String[size][];
            for (int i = 0; i < size; i++) {
                Detail info = list.get(i);
                data[i] = info.values();
    
            }
            int total = dataService.getCameraByTimeTotal(detail);
            
            PrintWriter out = null;
            try
            {
                String result = JSonUtils.toJSon();
                out = response.getWriter(); 
                out.print(result);
                out.flush();
            }
            catch (Exception e) {
              LOG.error("error",e);
            }
            finally
            {
                if (out != null) {
                    out.close();
                }
            }
        }

    解决方案为:将对象通过流的形式进行传输给前台,PrintWriter向文本输出流打印对象的格式化表示形式。

  • 相关阅读:
    对虚机设备Bridge ,Vlan, VETH, TAP详细介绍
    DevStack部署Openstack环境
    Ubuntu配置 PPTP 服务器端
    Ubuntu 配置PPTP客户端
    Git学习笔记
    Mysql安装随记,整理内容来源网络
    GitHub访问慢的优化处理
    NetCore部署到Linux服务器+Supervisor的步骤及过程中踩过的坑
    JavaScript的定时器如何先触发一次再延时
    在实现文本框只能输入数字和小数点的基础上实现了价格样式(保留两位小数)
  • 原文地址:https://www.cnblogs.com/zjdxr-up/p/8108531.html
Copyright © 2011-2022 走看看