JSONArray和JSONObject的详细使用:http://blog.csdn.net/yangbobo1992/article/details/8350765
/** * ajax调用返回json数组 * @param list */ public void printList(List<?> list){ HttpServletResponse response = ServletActionContext.getResponse(); //获取response实例 response.setContentType("text/html"); //设置输出类型 response.setCharacterEncoding("UTF-8"); //设置输出编码 /*JsonConfigFactory.getInstance(); JSONArray jsonArray = new JSONArray(); jsonArray.addAll(list, JsonConfigFactory.getInstance()); */ try { response.getWriter().print(JSONArray.fromObject(list, JsonConfigFactory.getInstance())); } catch (IOException e) { e.printStackTrace(); } } /** * ajax调用返回json对象 * @param obj */ public void printObject(Object obj){ HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); try { response.getWriter().print(JSONObject.fromObject(obj,JsonConfigFactory.getInstance())); } catch (IOException e) { e.printStackTrace(); } } /** * ajax调用返回json对象 * @param obj */ public void printSting(String str){ HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); try { response.getWriter().print(str); } catch (IOException e) { e.printStackTrace(); }
生命在于奋斗,也为自己带盐