ajax中解析json数据的各种方法(返回类型为json或text)

对于json

从servlet中返回给ajax的dataType类型可以是text,json

1、在servlet中构造json文本,再response.getWriter()给ajax,返回json text文本

servlet中,一面一段写在doPost或doGet中,但要相互调用this.doGet(rq,rs)或this.doGet(rq,rs)

用jquery的ajax异步提交,数据必须有一个明确的ID(不再是name,和form提交方式不一样),比如下面jsp中的username就是ID


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                response.setContentType("text/html;;charset=utf-8");//注意要是返回文本,那么类型为text/html
		String username1 = request.getParameter("username");
		String username = URLDecoder.decode(username1,"UTF-8");
		StringBuilder message = new StringBuilder("");
		if(username!=null)
		{
			EmployeeDao employeeDao = new EmployeeDao();
			ArrayList employees = employeeDao.select(username);
			
			if(employees!=null&&employees.size()>0)
			{
				 message.append("{\"emp\":[");
//				for(Employee employee: employees)
//				{
				 //对于这样的集合类型的数据,不推荐使用构造字符串的方式来构造json数组
//					System.out.println(employee.getEmpNo());
//					message.append("{\"eno\":"+employee.getEmpNo()+","+"\"ename\""+employee.getEname()+"}");
//				}
				 //为了说明构造json文本,仅使用2条记录
				 message.append("{\"eno\":"+"\""+employees.get(0).getEmpNo()+"\""+","+"\"ename\""+":"+"\""+employees.get(0).getEname()+"\""+"}");
				 message.append(",");
				 message.append("{\"eno\":"+"\""+employees.get(1).getEmpNo()+"\""+","+"\"ename\""+":"+"\""+employees.get(1).getEname()+"\""+"}");
				 message.append("]}");
			}
			 PrintWriter out = response.getWriter();
			 out.println(new String(message));
		}
		

在ajax中

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




Insert title here






	输入用户名
	
	

2、用字符串构造json数据,返回json对象

servlet中返回application/json

ajax中dateType用json


servlet代码,和上面没有什么区别,主要是把contentype改为application/json

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
		String username1 = request.getParameter("username");
		String username = URLDecoder.decode(username1,"UTF-8");
		StringBuilder message = new StringBuilder("");
		
		if(username!=null)
		{
			EmployeeDao employeeDao = new EmployeeDao();
			ArrayList employees = employeeDao.select(username);
			
			if(employees!=null&&employees.size()>0)
			{
				 message.append("{\"emp\":[");
//				for(Employee employee: employees)
//				{
				 //对于这样的集合类型的数据,不推荐使用构造字符串的方式来构造json数组
//					System.out.println(employee.getEmpNo());
//					message.append("{\"eno\":"+employee.getEmpNo()+","+"\"ename\""+employee.getEname()+"}");
//					
//				}
				 //为了说明构造json文本,仅使用2条记录
				 message.append("{\"eno\":"+"\""+employees.get(0).getEmpNo()+"\""+","+"\"ename\""+":"+"\""+employees.get(0).getEname()+"\""+"}");
				 message.append(",");
				 message.append("{\"eno\":"+"\""+employees.get(1).getEmpNo()+"\""+","+"\"ename\""+":"+"\""+employees.get(1).getEname()+"\""+"}");
				 
				 message.append("]}");
			}
			 	 System.out.println(message);
				 PrintWriter out = response.getWriter();
				 out.println(new String(message));
				
		}
		
		
	}
 

jsp中代码

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




Insert title here




	输入用户名
	
	


3、返回json对象,但是在servlet中直接用把一个数组或集合转换成为json类型的数组对象

如果抛出 NestableRuntimeException异常,

 说明没有导包,注意导入相关的jar包

commons-beanutils-1.8.3

commons-lang-2.6 (注:导入最新的 3.1 版本会继续报如下错误)

commons-collections-3.2.1

commons-logging-1.1.1

ezmorph-1.0.6

参考:http://blog.csdn.net/wugewuge/article/details/8074272

在servlet写法

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
		String username1 = request.getParameter("username");
		String username = URLDecoder.decode(username1,"UTF-8");
		StringBuilder message = new StringBuilder("");
		
		if(username!=null)
		{
			EmployeeDao employeeDao = new EmployeeDao();
			ArrayList employees = employeeDao.select(username);
			 JSONArray jsonArray=JSONArray.fromObject(employees);
			 PrintWriter out = response.getWriter();
			 out.println(jsonArray.toString());//在jsp中的数据类型dataType:"json"
				
		}
		
		
	}

在jsp中

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




Insert title here




	输入用户名
	
	




你可能感兴趣的:(ajax+json)