In-depth understanding of JavaScript (V)

Source: Internet
Author: User
Tags rfc

There's no "JSON object" at all! Objective

The purpose of this article is to often see 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.

Body

The topic of this article is written based on Ecmascript262-3, and the 2011 262-5 New specification adds JSON objects that are related to what we normally call JSON, but not the same thing, and the last section of the article talks about the new additions to the JSON object.

I would like to clarify a very common misconception, and I think many JavaScript developers mistakenly refer to JavaScript 对象字面量 (Object literals) JSON 对象 (JSON Objects), Because his syntax is the same as that described in the JSON specification, the specification also explicitly says that JSON is just one 数据交换语言 , which is called JSON only when we use it in the string context.

Serialization and deserialization

When 2 programs (or servers, languages, and so on) need to communicate interactively, they tend to use string strings because string is interpreted in many languages in the same way. Complex data structures are often used, and are made up of a wide variety of brackets {}, parentheses (), parentheses, <> and empty Glyd, which are just characters that are well-known in terms of specifications.

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!

Literal quantity

Quote a few words from Mozilla Developer Center for your reference:

    1. They are fixed values, not variables, that let you understand the script from "literally". (literals)
    2. String literals are made up of 0 or more characters enclosed by double quotation marks (") or single quotation marks ('). (Strings literals)
    3. The object literal is a property name-value pair of 0 or more objects enclosed in curly braces ({}). (Object literals)
When is JSON, when 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 is likely to be a JavaScript object literal may also be 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 J The SON string, if used in the context of the object literal, is the object literal.

// This is a JSON string var foo = ' {' prop ': ' Val '} ';
// This is the object literal . var bar = {"prop": "Val"};

Also note that JSON has a very strict syntax, in the string context {"prop": "Val"} is a valid JSON, but {prop: "Val"} and {' prop ': ' Val '} are 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 the context.

The big guy might scoff: is the JavaScript code not a large string?

Of course, all Javascrip T codes and HTML (and possibly other things) 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.

A true JSON object

As mentioned at the beginning, 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: Used to deserialize the JSON.parse JSON string into an JSON.stringify object 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 it, don't worry, just take a look at the example:

// This is a JSON string, such as getting string information from Ajax var my_json_string = ' {' prop ': ' Val '} ';
// deserializing a string into an object var my_obj =//   hint True, as imagined!
// serializes an object into a JSON string var my_other_json_string = json.stringify (my_obj);

In addition, Paul Irish mentions that Douglas Crockford uses "JSON object" in the JSON RFC, but in that context he means "objects are described as JSON strings" and not "object literals".

More information

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

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

In-depth understanding of JavaScript (V)

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.