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:
< 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的网页里使用