Introduction
We know that AJAX technology can make every request faster. For each request, the returned data is not the whole page, but also the data to be returned. Ajax usually returns XML format data, and then parses and renders the XML format data through complicated JavaScript scripts on the client.
JSON (read Jason) is used to make the data format a standard, which is simpler to be parsed by JavaScript.
Advantages
1. lightweight data exchange format
2. easier reading and writing
3. Easy machine resolution and generation
4. JSON parsing through eval () function in Javascript
5. JSON supports multiple languages. Including: ActionScript, C, C #, ColdFusion, E, Java, JavaScript, ML, objective caml, Perl, PHP, Python, REBOL, Ruby, and Lua.
Syntax
JSON syntax is a protocol used to transmit and generate data. It is similar to the C language, so it is easily parsed by the C language.
Object: the object is included {}
Attribute: the key-value pair is used. Use commas to separate attributes. String: Value
Array: The array is stored between [] [elements]
Elements: separated by commas
Value: The value can be a string, number, object, array, true, false, or null.
Example:
JSON
[
{
Name: "Michael ",
Email: 17bity@gmail.com ",
Homepage: "http://www.jialing.net"
},
{
Name: "John ",
Email: john@gmail.com ",
Homepage: "http://www.jobn.com"
},
{
Name: "Peggy ",
Email: peggy@gmail.com ",
Homepage: "http://www.peggy.com"
}
]
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 = "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. To generate JavaScript with the correct syntax on the server sideCodeIt is not easy to do. This mainly happens in a relatively large system, with different developers on the server and client.