Ajax——json
json是js提供的一种数据交换格式! json的语法 应用json var person = {"name":"zhangSan","age":18,"sex":"male"}; json与xml比较 package servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.junit.Test; public class AServlet extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { response.setContentType("text/html;charset=utf-8"); String str="{"name":"Zhangsan","age":16,"sex":"male"}"; response.getWriter().print(str); System.out.println(str); } } <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'json1.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript"> function createXMLHttpRequest(){ try { return new XMLHttpRequest(); } catch (e) { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { return new ActiveXObject("MicroSoft.XMLHTTP"); } catch (e) { // TODO: handle exception } } } } window.onload=function(){ var bt=document.getElementById("bt"); bt.onclick=function(){ var xmlHttp=createXMLHttpRequest(); xmlHttp.open("GET","<c:url value='/AServlet' />",true); xmlHttp.send(null); xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4 && xmlHttp.status==200){ var text=xmlHttp.responseText; var person=eval("("+text+")"); var s=person.name+","+person.age+","+person.sex; document.getElementById("h1").innerHTML=s; } }; }; }; </script> </head> <body> <h1>json演示</h1> <button id="bt">点击这里</button> <h1 id="h1"></h1> </body> </html>json-lib可以把javabean转换成json串 核心类 package demo; import java.util.ArrayList; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.junit.Test; public class Demo { @Test public void fun1(){ JSONObject map=new JSONObject(); map.put("name","ZHangsan"); map.put("age",16); map.put("sex","male"); String s = map.toString(); System.out.println(s); } @Test public void fun2() { Person p=new Person("Lisi",18,"male"); //把对象转换成JSONObject对象 JSONObject map=JSONObject.fromObject(p); String s = map.toString(); System.out.println(s); } @Test public void fun3() { Person p1=new Person("Lisi",55,"male"); Person p2=new Person("haha",33,"male"); JSONArray list=new JSONArray(); list.add(p1); list.add(p2); String s = list.toString(); System.out.println(s); } @Test public void fun4() { Person p1=new Person("Lisi","male"); List<Person> list=new ArrayList<Person>(); list.add(p1); list.add(p2); System.out.println(JSONArray.fromObject(list)); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |