In the previous article, "Exploring the message transfer model in AJAX (i)", I mainly introduced the common string (Plain-text string) and the XML format transmission mode, however, in practical development applications, these two methods are basically enough to meet our needs, However, in the case of complex objects, the two transmission modes described above are somewhat unsatisfactory. In this context, this article will combine the "explore the message transfer model in Ajax" (a) to introduce a lightweight data Interchange Format JSON (JavaScript object notation), which is a javascrpt of its own to describe the object of the method, JSON can be said to be an alternative to XML from a certain point of view.
Before using JSON for data transfer, let's look at a few simple JSON syntaxes to lay the groundwork for friends who are unfamiliar with JSON and want to see this article. JSON is a simple text format as well as XML. Compared with XML, it is more readable and easier to inspect. On the grammatical level, the difference between JSON and other formats is the character that separates the data, and the delimiters in JSON are limited to single quotes, parentheses, brackets, braces, colons, and commas. The following is a JSON payload:
{"UserID":"0001","UserName":"ZhangSan","UserAge":"22"}
Looks like it's not very simple, the key is corresponding to the value one by one (key----value), let's look at the JSON payload for a complex point:
1{Employees:[
2 {"EmployeeID":"1","LastName":"Davolio","City":"Seattle","Country":"USA"},
3 {"EmployeeID":"2","LastName":"Fuller","City":"Tacoma","Country":"USA"}
4 ]
5}
It is clear from the JSON above that there are two of data in the Employees object, which we rewrite with XML as follows:
1<?xml version='1.0' ?>
2<Employees>
3 <Employee>
4 <EmployeeID>1</EmployeeID>
5 <LastName>Davolio</LastName>
6 <City>Seattle</City>
7 <Country>USA</Country>
8 </Employee>
9 <Employee>
10 <EmployeeID>2</EmployeeID>
11 <LastName>Fuller</LastName>
12 <City>Tacoma</City>
13 <Country>USA</Country>
14 </Employee>
15<Employees>