Home > Web Front-end > JS Tutorial > body text

After reading it, you will know what JSON_json is

WBOY
Release: 2016-05-16 19:07:18
Original
1160 people have browsed it

What is JSON
JSON (Javascript Object Notation) is a lightweight data exchange format. Easy for humans to read and write. It is also easy for machines to parse and generate. It is based on Javascript Programming Language, a subset of Standard ECMA-262 3rd Edition - December 1999. JSON uses a completely language-independent text format, but also uses conventions similar to the C language family (including C, C, C#, Java, Javascript, Perl, Python, etc.). These properties make JSON an ideal data exchange language.
The concept of JSON is very simple, that is, the server directly generates Javascript statements, and the client directly uses the eval method to obtain the object after obtaining it, thus eliminating the cost of parsing XML.

is expressed in XML:

Copy code The code is as follows:

< items>                                                                     http://www.jb51.net< /url>                                                                                                                 ;/id> 
 < ;author>Relkn 
 
 
                                                                 lt;url>http://www.jb51 .net                                                                                                             :




Copy code


The code is as follows:

 
     
        1 
        Jackson 
        http://www.jb51.net 
        Welcome to Web.cn 
    
 
     
        2 
        Relkn 
        http://www.jb51.net 
        Web.cn关注互联网新技术 
    
 
     
        3 
        Kvogend 
        http://www.jb51.net 
    jb51.net脚本之家 
    
 
  


使用JSON:   
复制代码 代码如下:

{items:[    
{    
id:1,    
author:"Jackson",   
url:"http://www.jb51.net",   
content:"Welcome to Web.cn"   
},   
{   
id:2,   
author:"Relkn",   
url:"http://www.jb51.net",   
content:"Web.cn关注互联网新技术"   
},   
{   
id:3,   
author:"Kvogend",   
url:"http://www.jb51.net",   
content:"jb51.net脚本之家"   
}    
]};    

代码:  
复制代码 代码如下:

{items:[ 

id:1, 
author:"Jackson", 
url:"http://www.jb51.net", 
content:"Welcome to Web.cn" 
}, 

id:2, 
author:"Relkn", 
url:"http://www.jb51.net", 
content:"Web.cn关注互联网新技术" 
}, 

id:3, 
author:"Kvogend", 
url:"http://www.jb51.net", 
content:"jb51.net脚本之家" 

]};  


JSON不仅减少了解析XML解析带来的性能问题和兼容性问题,而且对于Javascript来说非常容易使用,可以方便的通过遍历数组以及访问对象属性来获取数据,其可读性也不错,基本具备了结构化数据的性质。不得不说是一个很好的办法,而且事实上google maps就没有采用XML传递数据,而是采用了JSON方案。

JSON的另外一个优势是"跨域性",例如你在www.jb51.net的网页里使用

http://www.yyy.com/some.js">

是完全可行的,这就意味着你可以跨域传递信息。而使用XMLHttpRequest却获取不了跨域的信息,这是Javascript内部的安全性质所限制的。

JSON能完全取代XML吗?当然不能,原因就在于XML的优势:通用性。要使服务器端产生语法合格的Javascript代码并不是很容易做到的,这主要发生在比较庞大的系统,服务器端和客户端有不同的开发人员。它们必须协商对象的格式,这很容易造成错误。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!