Deep understanding of the JavaScript series (9) There's no "JSON object" at all! _javascript Tips

Source: Internet
Author: User
Tags data structures rfc
Objective
The purpose of this article is to often see developers say: To convert 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 in the translation of the text for everyone to discuss, if there is a mistake, please point out, thank you.

Body
The subject of this article is based on ecmascript262-3, and the 262-5 new specification in 2011 adds JSON objects, which are related to our usual JSON, but not the same thing, and the last section of the article talks about the newly added JSON object.

Original English: http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/
I want to clarify a very common misconception that many JavaScript developers mistakenly refer to JavaScript object literals (object literals) as JSON objects (JSON Objects). Because of his syntax as described in the JSON specification, it is also clear from the specification that JSON is just a data exchange language, and it is called JSON only when we use it in the string context.

Serialization and deserialization
When 2 programs (or servers, languages, etc.) need to be communicated interactively, they tend to use string strings because the string is parsed in much the same way in many languages. Complex data structures often need to be used, and are composed of various brackets {}, parentheses (), brackets <> and empty cells, a string that is just the character that is required to be normalized.

To this end, we have developed standard rules and syntax to describe these complex data structures as a string. 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 into the desired format. Yaml and XML (and even request params) are popular data interchange formats, but we like JSON, who calls US JavaScript developers!

Literal amount
Quote 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 composed of 0 or more characters surrounded by double quotes (") or single quotes ('). (Strings literals)
An object literal is a property name-value pair of 0 or more objects enclosed in braces ({}). (Object literals)
When is JSON, and when is 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 might be a JavaScript object literal or a JSON string, depending on what context uses it, if it is used in a string context (enclosed in single or double quotes, or read from a text file), So it's the JSON string, and if it's in the object literal context, it's the object literal.
Copy Code code as follows:

This is a JSON string
var foo = ' {prop ': ' Val '} ';

This is the literal amount of the object
var bar = {"prop": "Val"};

Also note that JSON has very strict syntax, in the string context {"prop": "Val"} is a valid JSON, but {prop: "Val"} and {' prop ': ' Val '} are really illegal. All property names and its values must be enclosed in double quotes and cannot be used in single quotes. In addition, even if you use the escaped single quotation mark is not legal, detailed syntax rules can be viewed here.

Put it in the context.
Big guy might scoff: isn't JavaScript code a big string?

Of course, all the JavaScript code and HTML (and possibly other things) are strings until the browser parses them. At this point, the JF file or inline JavaScript code is not a string, but is considered as the real JavaScript source code, like the innterhtml in the page, this time is not a string, but is parsed into a DOM structure.

Again, depending on the context, the JavaScript object with curly braces is used 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
As mentioned at the beginning, the object literal is not a JSON object, but there is a true JSON object. But the two are completely different concepts, in the new browser, the JSON object has been the original built-in object, 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 do not understand, do not worry, refer to the example to know:
Copy Code code as follows:

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 '); Hint true, as imagined!

Serializing an object into a JSON string
var my_other_json_string = json.stringify (my_obj);

In addition, Paul Irish mentions that Douglas Crockford used "JSON object" in the JSON RFC, but in that context, he means "object description as JSON string" is not "object literal."

More information
If you want to learn more about JSON, the following links are absolutely useful to you:

    • JSON specification
    • JSON RFC
    • JSON on Wikipedia
    • Jsonlint-the JSON Validator
    • JSON is not the same as JSON
Related Article

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.