JSON是Javascript表示法:JavascriptObject Notation。
JSON是存储和交换文本信息的语法,类似XML。
JSON比XML更小、更快、更易解析。
JSON独立于语言。
2.使用转换为Javascript对象
JSON 文本格式在语法上与创建 Javascript 对象的代码相同,无需解析器,Javascript程序能够使用内建的eval() 函数,用 JSON 数据来生成原生的 Javascript 对象。
3.DemoJson1.html
Json2.html
Json3.html
Json4.html
Json5.htm
通过 JSON 字符串来创建对象4.JSON文件First Name:
Last Name:
JSON 文件的文件类型是 ".json"
JSON 文本的 MIME 类型是"application/json"
5.JSON解析器提示:eval() 函数可编译并执行任何 Javascript 代码。这隐藏了一个潜在的安全问题。
使用 JSON 解析器将 JSON 转换为 Javascript 对象是更安全的做法。JSON 解析器只能识别 JSON 文本,而不会编译脚本。
在浏览器中,这提供了原生的 JSON 支持,而且 JSON 解析器的速度更快。
较新的浏览器和最新的 ECMAscript(Javascript) 标准中均包含了原生的对 JSON 的支持。
Web 浏览器支持 | Web 软件支持 |
·Firefox (Mozilla) 3.5 ·Internet Explorer 8 ·Chrome ·Opera 10 ·Safari 4 | ·jQuery ·Yahoo UI ·Prototype ·Dojo ·ECMAscript 1.5 |
Json6.html
通过 JSON 字符串来创建对象First Name:
Last Name:
*如果出现“JSON未定义”,引用附件json.js可解决。
6.Jquery+Servlet+Json(检测用户名是否可用的一个实例)Login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
AJAX....
Web.xml
userTest com.zpl.servlet.userNameTestAction userTest /userNameTestAction
userNameTestAction.java
public class userNameTestAction extends HttpServlet{ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String userName = request.getParameter("uuu"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); if("zhangsan".equals(userName)){ System.out.println("用户名已存在!"); out.print("用户名已存在!"); }else{ System.out.println("用户名可用!"); out.print("用户名可用!"); } } }7.Jquery+Servlet+Json实例Login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
AJAX....
userNameTestAction.java
public class userNameTestAction extends HttpServlet{ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); JSONObject jsonObject = null; Person p1 = new Person(1,"zhangsan"); jsonObject = JSONObject.fromObject(p1); String json = jsonObject.toString(); System.out.println(json); out.print(json); //String str = "{‘name’:‘zhangsan’}";//为什么单引号不可以呢?不科学啊 //String str = "{"name":"sb"}"; //out.print(str); //System.out.println(str); } }Person.java
public class Person { private int id; private String name; public Person(){ } public Person(int id, String name) { super(); this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}Web.xml
userTest com.zpl.servlet.userNameTestAction userTest /userNameTestAction



