map转json, 使用ajax时, 利用map向前台返回一些数据。 或者向app返回数据使用
直接上代码
private void mapToResponse(Map param) {
String callback = this.getRequest().getParameter("callback");
JSONObject jsonObject = JSONObject.fromObject(param);
String content;
if(callback!=null&&!callback.equals("")){
content = callback+"("+jsonObject+")";
}else{
content = jsonObject.toString();
}
HttpServletResponse httpResponse = this.getResponse();
httpResponse.setContentType("text/html;charset=utf-8");
PrintWriter out = null;
try {
out = httpResponse.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
if (out != null) {
out.write(content);
out.flush();
out.close();
}
}