In-depth understanding of the JavaScript series (9): There is no "JSON object" at all!

Source: Internet
Author: User

Preface The purpose of this article is to see often developers say: the string into a JSON object, the JSON object into a string and other similar topics, so the previous collection of a foreigner's article collation translated for everyone to discuss, if there are errors, please point out, thank you. The main topic of this article is based on ECMAScript262-Written in 2011, 262-5 The new specification adds JSON objects, which are related to what we normally call JSON, but not the same thing, and the last section of the article will talk about the newly added JSON object. English Original: http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/I would like to clarify a very common misconception that I think many JavaScript developers mistakenly refer to the JavaScript object literal (object literals) as a JSON object (JSON Objects). Because his syntax is the same as that in the JSON specification, the specification explicitly says that JSON is just a data exchange language, which is called JSON only when we use it in the string context. When serializing and deserializing 2 programs (or servers, languages, and so on) need to communicate interactively, they tend to use string strings because string is parsed in many languages in the same way. Complex data structures often need to be used, and through a variety of brackets {}, parentheses (), called parentheses<>and an empty Glyd, this string is just a character that is well-regulated. For this reason, we have developed standard rules and syntax for describing these complex data structures as a string literal. JSON is just one of the syntaxes that can describe objects, arrays, strings, numbers, Booleans, and nulls in a string context, and then transfer between programs and deserialize to the desired format. Yaml and XML (even request params) are popular data interchange formats, but we like JSON, who calls US JavaScript developers! The literal reference to a few words from Mozilla Developer Center for your reference: they are fixed values, not variables, allowing you to "literally" understand the script. (literals) string literals are double quotation marks (") or a single quotation mark (') that is surrounded by 0 or more characters. (Strings literals) The object literal is a property name-value pair of 0 or more objects enclosed in curly braces ({}). When is (Object literals) JSON, and when is it not json? JSON is designed to describe the data interchange format, and he has his own syntax, which is a subset of JavaScript. { "Prop": "Val"such a declaration could be a JavaScript object literal or a JSON string, depending on what context uses it, if it is used in a string context (in single or double quotation marks, or read from a text file), then it is a JSON string, If it is used in the context of the object literal, it is the object literal. This is the JSON string var foo = ' {"Prop": "Val"} '; This is the object literal var bar = {"Prop": "Val"and note that JSON has a very strict syntax, in the string context {"Prop": "Val"} is a valid JSON, but {prop: "Val"} and {' prop ': ' Val '} is indeed illegal. All property names and their values must be enclosed in double quotation marks and cannot be used in single quotes. In addition, even if you use the escape after the single quotation mark is also illegal, the detailed syntax rules can be viewed here. Put it in context and look at the big guy. It's possible to scoff: isn't the JavaScript code a big string? Of course, all the JavaScript code and HTML (and possibly anything else) are strings until they are parsed by the browser. At this time, the. jf file or inline JavaScript code is not a string, but rather as a real JavaScript source code, like the page innterhtml, this time is not a string, but is parsed into the DOM structure. Again, it depends on the context, using a JavaScript object with curly braces in the string context, which is the JSON string, and if used in the object literal context, it is the object literal. The true JSON object has been mentioned at the beginning, and the object literal is not a JSON object, but there is a real JSON object. But the two are completely different concepts, in the new version of the browser JSON objects have been native built-in objects, there are currently 2 static methods: Json.parse used to deserialize the JSON string into an object, json.stringify used to serialize the object into a JSON string. The old version of the browser does not support this object, but you can do the same with Json2.js. If you don't understand, don't worry, take a look at the example://This is a JSON string, such as getting string information from ajax var my_json_string = ' {"Prop": "Val"} '; Deserializes a string into an object var my_obj = Json.parse (my_json_string); Alert (My_obj.prop = = ' val '); Prompt true, as imagined! Serializes the object to a JSON string var my_other_json_string = json.stringify (my_obj); In addition, Paul Irish mentions that Douglas Crockford uses the JSON Object ", but in that context, he means" the object is described as a JSON string "instead of" object literal. "  More information if you want to learn more about JSON, the following connection is absolutely useful for you: JSON Specificationjson Rfcjson on wikipediajsonlint-the json Validatorjson are not the Same as JSON synchronization and recommendation this article has been synchronized to the directory index: in-depth understanding of JavaScript series in-depth understanding of the JavaScript series, including the original, translation, reprint and other types of articles, if it is useful to you, please recommend supporting a, to the power of the uncle writing. 

In-depth understanding of the JavaScript series (9): There is no "JSON object" at all!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.