首页 > 代码库 > Ajax调用返回json数组,对象 (JSONArray.fromObject)
Ajax调用返回json数组,对象 (JSONArray.fromObject)
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(); }
Ajax调用返回json数组,对象 (JSONArray.fromObject)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。