您的位置:首页 > Web前端 > JavaScript

Response JSON数据返回

2015-08-16 04:43 645 查看
简述:

在servlet填充Response的时候,做JSON格式的数据转换

使用的类是net.sf.json.JSONObject,传入response对象和返回的显示类,修改response,返回前台JSON格式数据

/**
* 以JSON格式输出
* @param response
*/
protected void responseOutWithJson(HttpServletResponse response,
Object responseObject) {
//将实体对象转换为JSON Object转换
JSONObject responseJSONObject = JSONObject.fromObject(responseObject);
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter out = null;
try {
out = response.getWriter();
out.append(responseJSONObject.toString());
logger.debug("返回是\n");
logger.debug(responseJSONObject.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: