What is JSON
JSON (Javascript Object Notation) is a lightweight data exchange format. Easy to read and write. It is also easy to parse and generate machines. It is based on a subset of Javascript Programming Language, Standard ECMA-262 3rd Edition-December 1999. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, Javascript, Perl, Python, and so on ). These features make JSON an ideal data exchange language.
The JSON concept is very simple, that is, the server directly generates Javascript statements, and the client directly uses the eval method to obtain this object after obtaining the object, which can save the parsing XML loss.
In XML format:
Copy codeThe Code is as follows:
<Items>
<Item>
<Id> 1 </id>
<Author> Jackson </author>
<Url> http://www.jb51.net </url>
<Content> Welcome to Web.cn </content>
</Item>
<Item>
<Id> 2 </id>
<Author> Relkn </author>
<Url> http://www.jb51.net </url>
<Content> Web.cn focus on new Internet technologies </content>
</Item>
<Item>
<Id> 3 </id>
<Author> Kvogend </author>
<Url> http://www.jb51.net </url>
<Content> jb51.net script home </content>
</Item>
</Items>
Code:
Copy codeThe Code is as follows:
<Items>
<Item>
<Id> 1 </id>
<Author> Jackson </author>
<Url> http://www.jb51.net </url>
<Content> Welcome to Web.cn </content>
</Item>
<Item>
<Id> 2 </id>
<Author> Relkn </author>
<Url> http://www.jb51.net </url>
<Content> Web.cn focus on new Internet technologies </content>
</Item>
<Item>
<Id> 3 </id>
<Author> Kvogend </author>
<Url> http://www.jb51.net </url>
<Content> jb51.net script home </content>
</Item>
</Items>
Use JSON:
Copy codeThe Code is as follows:
{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 following new Internet technologies \"
},
{
Id: 3,
Author: \ "Kvogend \",
Url: \ "http://www.jb51.net \",
Content: \ "jb51.net script house \"
}
]};
Code:
Copy codeThe Code is as follows:
{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 following new Internet technologies \"
},
{
Id: 3,
Author: \ "Kvogend \",
Url: \ "http://www.jb51.net \",
Content: \ "jb51.net script house \"
}
]};
JSON not only reduces the performance and compatibility issues caused by parsing XML parsing, but also is very easy to use for Javascript. It is convenient to retrieve data by traversing arrays and accessing object attributes, its readability is good, and it basically has the nature of structured data. I have to say that it is a good method. In fact, google maps does not use XML to transmit data, but uses the JSON scheme.
Another advantage of JSON is "cross-origin". For example, you can use
<Script type = "text/javascript" src = "" target = "_ blank"> http://www.yyy.com/some.js ">
This means that you can transmit information across domains. However, XMLHttpRequest cannot obtain cross-origin information, which is restricted by the security nature of Javascript.
Can JSON completely replace XML? Of course not. The reason is the advantage of XML: versatility. It is not easy for the server to generate Javascript code with the correct syntax. This mainly happens in a large system, where different developers are working on the server and the client. They must negotiate the format of the object, which can easily cause errors.