JSON
1.json是什么?
*它是js提供的一种数据交换格式
2.json的语法
*{}:是对象!
>属性名必须使用双引号括起来!单引号不行!!!
>属性值:
*null
*数值
*数组,使用[]括起来
*对象
*字符串
*boolean值
3.应用json
*var person={"name":"zhangsan","password":"961012gz","age":12};
函数eval():
将字符串当成js代码执行,一般格式为: eval("("+str+")");返回的是一个对象
4.json与xml比较
*可读性:xml胜出
*解析难度:json主场优势
*流行度:xml流行很多年
-------------------------------
json-lib
1、是什么?可以将json数据转换成json字符串
2、导入jar包 略
3、核心类
*JSONObject--》Map
>toString()
>JSONObject map=JSONObject.fromObject(person); 把对象转换成JSONObject独享
*JSONArray-->List
>toString
>JSONArray jsonArray=JSONObject.fromObject(list):把list转化成为JSONArray对象
了解json数据的小例子:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% 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"> window.onload=function(){ //var person={"name":"guozhen","age":12,"sex":"男"}; //alert(person.name+" ,"+person.age+" ,"+person.sex); //var str="{"name":"guozhen","age":12,"sex":"男"}"; //var person=eval("("+str+")"); //alert(person.name+" ,"+person.age+" ,"+person.sex); var str="1+2"; var num=eval("("+str+")"); alert(num); } </script> </head> <body> <h1>json的简单应用!</h1> </body> </html>
使用json数据类型进行简单异步刷新:
<%@ 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 'json2.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"> window.onload=function(){ var btn=document.getElementsByTagName("button")[0]; btn.onclick=function(){ var xmlHttp=createXMLHttpRequest(); xmlHttp.open("GET","<c:url value='/AServlet'/>",true); xmlHttp.send(null); xmlHttp.onreadystatechange=function(){ if(xmlHttp.status==200 && xmlHttp.readyState==4){ var text=xmlHttp.responseText; var person=eval("("+text+")"); var h3=document.getElementsByTagName("h3")[0]; h3.innerHTML="name:"+person.name+",age:"+person.age+",sex:"+person.sex; } }; }; }; function createXMLHttpRequest(){ try{ return new XMLHttpRequest(); }catch(e){ try{ return new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try{ return new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ alert("哥们,你用的什么浏览器啊!"); throw e; } } } } </script> </head> <body> <button>点击我</button> <h1>JSON之Hello World</h1> <h3></h3> </body> </html>
package cn.itcast.web.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AServlet extends HttpServlet{ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String str="{"name":"zhangSan","age":18,"sex":"male"}"; resp.getWriter().print(str); } }
使用json-lib的操作例子:
package cn.itcast.demo1; import java.util.ArrayList; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.junit.Test; public class Demo1 { /** * JSONObject */ /* * 当map用 */ @Test public void fun1(){ JSONObject map=new JSONObject(); map.put("name", "zhangSan"); map.put("age", 23); map.put("sex","male"); String s=map.toString(); System.out.println(s); } /* * 当你已经有一个Person对象时,可以把Person转成JSONObject对象 */ @Test public void fun2(){ Person p=new Person("liSi",32,"male"); //把对象转换成JSONObject类型 JSONObject s=JSONObject.fromObject(p); System.out.println(s); } /** * JSONArray */ @Test public void fun3(){ Person p1=new Person("zhanSan",32,"male"); Person p2=new Person("liSi",23,"women"); JSONArray list=new JSONArray(); list.add(p1); list.add(p2); System.out.println(list); } /*将对象集合转换成为JSONArray*/ @Test public void fun4(){ Person p1=new Person("zhanSan",32,"male"); Person p2=new Person("liSi",23,"women"); List<Person> li=new ArrayList<Person>(); li.add(p1); li.add(p2); System.out.println(JSONArray.fromObject(li)); } public static void main(String[] args) { Demo1 d=new Demo1(); d.fun4(); } }
package cn.itcast.demo1; public class Person { private String name; private int age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Person() { super(); // TODO Auto-generated constructor stub } public Person(String name, int age, String sex) { super(); this.name = name; this.age = age; this.sex = sex; } }