What is JSON
JSON (JavaScript Object notation Http://www.json. org/json-zh.html) is a lightweight text-based, language-independent data interchange format that is lighter than XML. It is an alternative to XML data interchange. It derives from the subset of the ECMAScript programming language Standard-3rd edition (ECMA-262 3rd Edition-december 1999), defines a set of format specifications that facilitate presentation of structured data, and the JSON specification conforms to the ECMAScript syntax specification, The string that is described by the JSON specification is already the native code string of JavaScript, allowing it to be dynamically converted between the JSON string and the JavaScript object via eval. If exaggerated, it is another ideal but different from the XML data exchange language. JSON is constructed in two different structures
A collection of name/value pairs (A collection of name/value pairs). In different languages, it is understood as objects (object), records (record), structure (struct), dictionaries (dictionary), hash tables (hash table), a list of keys (keyed list), or associative arrays (associative Array).
The ordered list of values (an ordered list of values). In most languages, it is understood as an array.
These are common data structures. In fact most modern computer languages support them in some way. This makes it possible for a data format to be exchanged between programming languages that are also based on these constructs. JSON syntax rules
Object is an unordered set of ' name/value pairs '. An object begins with "{" (opening parenthesis), and "}" (closing parenthesis) ends. Each "name" is followed by a ":" (a colon), and the ' name/value ' pair is separated by a ', ' (comma).
An array is an ordered collection of values (value). An array begins with "[" (left bracket), and "]" (right bracket) ends. Values are separated by the "," (comma) value.
Values (value) can be enclosed in double quotes (string), numeric (number), True, False, NULL, objects (object), or arrays (array). These structures can be nested.
A string is a collection of any number of Unicode characters enclosed by double quotes, which is escaped using backslashes. A character (character) is a separate string (character string). Very similar to C or Java strings. Quick view of JSON and XML representations
If you have an employee object that has information such as "Last name, first name, employee number, title," See How JSON describes it: JS code { employee : { firstName: "John", lastName : "Doe", employeeNumber : 123, title : "Accountant" } }
To see how XML is represented, see: XML code <employee> <firstName>John</firstName> <lastname>doe</las tname> <employeeNumber>123</employeeNumber> <title>Accountant</title> </employ Ee>
From what is described above, JSON notation is not exactly a way of describing objects in JavaScript. Right, this is exactly the native code of the object constructs in JavaScript. Since it's a native code, we convert it to an object in JavaScript so that we manipulate the object much more conveniently than manipulating the string.
converts JSON strings into JavaScript objects: JS code <script type= "Text/javascript" > //parses JSON strings into objects using the script itself's function eval var e = eval ( ' ({' + ' employee : ' + ' {' + ' firstname: John ', ' + ' lastname : "Doe", ' + ' employeenumber : 123, ' + ' title : ' accountant ' + '} ' + '}] ' ); //Now we can use the object of E, You can also access the object's properties in the form of point access &NBSp; alert (e.employee.firstname); alert (e.employee.lastName ); alert (e.employee.employeenumber); Alert (e.employee.title); </script>
We'll compare them after reading the above.
XML vs. JSON
After some quick browsing how. Feel that no JSON design is more lightweight and concise than XML. It was previously said that it fits the characteristics of the JavaScript language object itself, which makes it possible to parse the Eval method if the text from the server is a string that conforms to the JavaScript syntax definition statement. That's true!
From the above two representations, JSON notation is much simpler in syntax than XML, and since there is no need to use a closing tag to echo the start tag, many redundant information is no longer present, and there is essentially no data redundancy relative to XML, which is greatly improved in terms of transmission and response speed.
In addition, JSON does not only have such advantages in the form of representation, but it is important to discard the previously confusing DOM parsing (the client's JavaScript XML DOM parsing, server-side DOM, SAX, dom4j, Jdom, and so on). JSON is much more versatile than XML, and a JSON-formatted data is converted into a JavaScript object for program invocation by a simple method of JavaScript (eval). The conversion method is that the JavaScript inside of the browser is well defined and not written manually. XML-formatted data needs to be parsed before it can be used by the XML parser tool inside the browser. and for different kernel browser (ie, Netscape, etc.) XML parsing method is different, so the need for different browser kernel to do a variety of methods of encapsulation, so that the client development brings a certain degree of complexity. In contrast, JSON is parsed faster by the browser. There are also different JSON parsers in different languages on the server side, and it is convenient to parse the strings sent by the client, rather than to read XML or use API tools like this or that. JSON pros and cons
Advantages:
At first glance, the advantages of using JSON data delimiters may not be obvious, but there is a fundamental reason: they simplify data access. When using these data delimiters, the JavaScript engine's internal representation of data structures, such as strings, arrays, objects, is exactly the same as those symbols.
Another advantage of JSON is its non verbose nature. In XML, it is necessary to open and close tags in order to satisfy markup compliance, and in JSON all these requirements can be satisfied with just a simple parenthesis. In data interchange with hundreds of fields, traditional XML tags will extend the data exchange time
In addition, JSON is favored by developers who excel in different programming languages. This is because development can easily generate JSON whether in Haskell or Lisp or in more mainstream C # and Java.
Insufficient:
Like many good things have two sides, JSON is not verbose, for this reason JSON lost some of the attributes of XML. Namespaces allow the same pieces of information in different contexts to be mixed with each other, however, it is clear that namespaces are not found in JSON. Another difference between JSON and XML is the difference in attributes, because JSON is assigned a colon, which results in a difficult distinction between identifiers (XML CDATA) and actual attribute values when XML is converted to JSON, and who should be considered as text.
In addition, the creation and validation of JSON fragments are slightly more complex than normal XML. From this point of view, XML is ahead of JSON in development tools. JSON Practice Preliminary Knowledge Dynamic script Function eval ()
Before we go any further, we need to explain the usage of the Eval method, which can be skipped by the students who know it.
The eval () function computes a string and executes the JavaScript code in it. It receives an argument s, and if S is not a string, it returns directly to S. Otherwise execute the S statement. If the S statement executes the result is a value, it returns this value directly, otherwise returns undefined.
In addition, the method accepts only the original string as a parameter, and if the string argument is not the original string, the method returns without any changes. Therefore, do not pass a String object for the eval () function as an argument: JS code var str = new String ("Alert (' msg ')"); Do not pass a string object, otherwise the string object is returned directly to alert (eval (str) ==str);//true//should pass the original string string so that it is considered a JavaScript script and executes the eval ("alert" ' msg ') ");//msg
Finally, it is necessary to note that the object declaration syntax "{}" does not return a value that needs to be enclosed in parentheses to return the value (the script in parentheses is an expression with a return value rather than a logical return value). Because the script in the curly braces may be an expression, and it may be a normal logical expression, so in parentheses around the explicit description is a value expression): JS code var str= "{}";//curly braces with no attributes when Alert (eval (' + str + '));/Popup:[object object] alert (eval (str)); Pop-up:undefined str= "{name: ' Jzj '}";//curly braces have a property alert (Typeof eval