这是在servlet中返回Json数据的格式,如果是在Struts框架中的action中返回json数据的话:见下:
//配合注册页面的ajax,实现大学和省份联动效果.
public ActionForward getUniversity(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setCharacterEncoding("utf-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter out = response.getWriter();
String province_Id = request.getParameter("province_id");
System.out.println(province_Id);
String jsonStr = "{\"id\":\"10\",\"name\":\"陕西\"}";
if ("10".equals(province_Id)) {
out.println(jsonStr);
}
return null;
}
这段代码是自己写省市联动的时候的一段测试代码,由于这个页面并不需要转向,所以返回null.