本帖最后由 赵兵锋 于 2012-6-8 09:55 编辑
看看实际应用中的使用不同吧
AJAX-XML:
服务器端- out.println("<response>");
- for (Category c : list) {
- out.println("<category><categoryid>" + c.getId()
- + "</categoryid><categoryname>" + c.getName()
- + "</categoryname></category>");
- }
- out.println("</response>");
复制代码 在客户端收到的是下面这样的数据- <response>
- <category>
- <categoryid>1</categoryid>
- <categoryname>类别1</categoryname>
- </category>
- <category>
- <categoryid>2</categoryid>
- <categoryname>类别2</categoryname>
- </category>
- </response>
复制代码 AJAX-JSON:
服务器端:- response.setContentType("application/json;charset=gb18030");
- ArrayList<Person> list = new ArrayList<Person>();
- list.add(new Personn("张三",22));
- list.add(new Personn("李四",21));
- PrintWriter out = response.getWriter();
- Gson gson = new Gson();
- String json = gson.toJson(list);
- out.print(json);
- out.flush();
复制代码 客户端收到的数据如下:
[{name:"张三";age:22},{name:"李四";age:21}]
客户端解析(jquery):- $.post("/JiaoWu/SearchCourses",
- {
- "a":"1"
- },function(returnedData,status){
- for(var i=0;i<returnedData.length;i++){
- alert(returnedData[i].name+","+returnedData[i].age);
- }
- }
- );
复制代码 |