1、通过xml进行数据交换

 

2、通过json进行数据交换

(1)数组表示有序结构

(2)键值对表示对应关系

(3)在服务器端由对象转换为json字符串

①json-lib

JSONArray jsonArray = JSONArray.fromObject(users);

  

JSONObject jsonObject = JSONObject.fromObject(user);

②Gson,谷歌的一个json解析

Gson gson = new Gson();
gson.toJson(user);

③jackson方式解析

(4)在服务器端或者在servlet中如何返回一个json字符串?

public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		//指定返回数据类型为json
		response.setContentType("application/json;charset=UTF-8");
		User user = new User(98, "zhangsan", "222222222222");
		PrintWriter outPrintWriter = response.getWriter();
		outPrintWriter.print(new Gson().toJson(user));
	}

(5)在js中解析json,需要json2.js完成。

	var jsonString = xmlHttp.responseText;
    	var jsonObject = JSON.parse(jsonString);
    	alert(jsonObject.name);

 

posted on 2014-11-09 17:45  猿类的进化史  阅读(190)  评论(0)    收藏  举报