json是js提供的一种数据交换格式!
json的语法
* {}:是对象!
> 属性名必须使用双引号括起来!单引不行!!!
> 属性值:
* null
* 数值
* 字符串
* 数组:使用[]括起来
* boolean值:true和false
应用json
var person = {"name":"zhangSan","age":18,"sex":"male"};
json与xml比较
* 可读性:XML胜出
* 解析难度:JSON本身就是JS对象(主场作战),所以简单很多
* 流行度:XML已经流行好多年,但在AJAX领域,JSON更受欢迎。
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串
核心类
* JSONObject --> Map
> toString();
> JSONObject map = JSONObject.fromObject(person):把对象转换成JSONObject对象
* JSONArray --> List
> toString()
> JSONArray jsonArray = JSONObject.fromObject(list):把list转换成JSONArray对象
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)); } }