返回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向文本輸出流打印對象的格式化表示形式。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM