首页 > 编程 > Java > 正文

【JAVA-工具类】response写出json

2019-11-08 20:26:23
字体:
来源:转载
供稿:网友
public void convert2json(Object obj) {        String json = JSONObject.toJSONString(obj);        HttpServletResponse response = getResponse();        response.setContentType("application/json;charset=utf-8");        PRintWriter out = null;        try {            out = response.getWriter();            out.write(json);        } catch (IOException e) {            logger.error("BaseAction::writerJson catch exception:",e);        } finally {            if (out != null)                out.close();        }    }           public void writerJson(Object obj) {        String json = JSONObject.toJSONString(obj);        HttpServletResponse response = getResponse();        response.setContentType("application/json;charset=utf-8");        PrintWriter out = null;        try {            out = response.getWriter();            out.write(json);        } catch (IOException e) {            logger.error("BaseAction::writerJson catch exception:",e);        } finally {            if (out != null)                out.close();        }    }    public void writerArrayJson(Object obj) {        String json = JSONObject.toJSONString(obj);        HttpServletResponse response = getResponse();        response.setContentType("application/json;charset=utf-8");        PrintWriter out = null;        try {            out = response.getWriter();            out.write(json);        } catch (IOException e) {            logger.error("BaseAction::writerJson catch exception:",e);        } finally {            if (out != null)                out.close();        }    }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表