栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

JSON数据格式

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JSON数据格式

1.简介

JSON是Javascript表示法:JavascriptObject Notation。

JSON是存储和交换文本信息的语法,类似XML。

JSON比XML更小、更快、更易解析。

JSON独立于语言。

2.使用

转换为Javascript对象

JSON 文本格式在语法上与创建 Javascript 对象的代码相同,无需解析器,Javascript程序能够使用内建的eval() 函数,用 JSON 数据来生成原生的 Javascript 对象。

3.Demo

Json1.html


Json2.html


Json3.html


Json4.html


Json5.htm

    通过 JSON 字符串来创建对象    

        First Name:         Last Name:     

    
4.JSON文件

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/246430.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号