Home  >  Article  >  Web Front-end  >  In-depth Understanding of JavaScript Series (9) There is no such thing as a "JSON object"! _javascript skills

In-depth Understanding of JavaScript Series (9) There is no such thing as a "JSON object"! _javascript skills

WBOY
WBOYOriginal
2016-05-16 17:57:00888browse

Preface
The purpose of writing this article is that I often see developers saying: convert strings into JSON objects, convert JSON objects into strings and other similar topics, so I compiled and translated an article by a foreigner that I had collected previously. Here it is for everyone to discuss. If there are any mistakes, please point them out. Thank you.

Text
The topic of this article is written based on ECMAScript262-3. The new 262-5 specification in 2011 added JSON objects, which is related to what we usually call JSON, but it is not the same thing. , the last section of the article will talk about the newly added JSON object.

Original English text: http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/
I would like to clarify a very common Misunderstanding, I think many JavaScript developers mistakenly call JavaScript object literals (JSON Objects) because its syntax is the same as described in the JSON specification, but the specification also clearly states JSON is just a data exchange language, it is only called JSON when we use it in a string context.

Serialization and Deserialization
When two programs (or servers, languages, etc.) need to communicate interactively, they tend to use string strings because strings are parsed in similar ways in many languages. Complex data structures are often used, and are composed of various square brackets {}, parentheses (), brackets <> and spaces. This string is just a string of characters that are standardized as required.

To this end, we have developed standard rules and syntax for describing these complex data structures as a string. JSON is just one of the syntaxes. It can describe objects, arrays, strings, numbers, booleans, and null in a string context, and then transmit them between programs and deserialize them into the required format. YAML and XML (even request params) are also popular data exchange formats, but, we like JSON, who call us JavaScript developers!

Literals
Quoting a few sentences from the Mozilla Developer Center for your reference:

They are fixed values, not variables, allowing you to understand the script "literally". (Literals)
String literals are composed of zero or more characters surrounded by double quotes (") or single quotes ('). (Strings Literals)
Object literals are composed of curly braces ( {}) zero or more object property name-value pairs (Object Literals)
When is it JSON and when is it not JSON?
JSON is designed to describe a data exchange format. It also has its own syntax, which is a subset of JavaScript.
{ "prop": "val" } Such a declaration may be a JavaScript object literal or a JSON string, depending on the context in which it is used. If it is used in a string context (enclosed in single or double quotes, or read from a text file), it is a JSON string. If it is used in an object literal context, it is an object literal <.>

Copy code The code is as follows:
// This is a JSON string
var foo = '{ "prop": "val" }';

// This is an object literal
var bar = { "prop": "val" };

And please note that JSON has a very strict syntax. In the string context { "prop": "val" } is a legal JSON, but { prop: "val" } and { 'prop': 'val' } are indeed illegal. All attribute names and their values ​​must be enclosed in double quotes. Single quotes are not allowed. In addition, even if you use escaped single quotes, you can check here for detailed syntax rules. >
Put it in context
People may scoff: Isn’t JavaScript code a big string?

Of course it is, all JavaScript code and HTML (maybe other things) They are all strings until the browser parses them. At this time, the .jf file or inline JavaScript code is no longer a string, but is regarded as the real JavaScript source code, just like innterHTML in the page. It is not a string, but is parsed into a DOM structure.

Again, it depends on the context. If you use a JavaScript object with curly brackets in the string context, it is a JSON string. And if used in the context of object literals, it is an object literal.

Real JSON object
As mentioned at the beginning, object literals are not JSON objects, but there are real JSON objects. But the two are completely different concepts. In the new version of the browser, JSON objects have been converted into native built-in objects. There are currently two static methods: JSON.parse is used to deserialize JSON strings into objects, and JSON.stringify is used to deserialize JSON strings into objects. Serialize the object into a JSON string. Older versions of browsers do not support this object, but you can achieve the same functionality through json2.js.

If you still don’t understand, don’t worry, just refer to the example:


Copy the code The code is as follows :

// This is a JSON string, such as getting string information from AJAX
var my_json_string = '{ "prop": "val" }';

// Convert the string Deserialize into object
var my_obj = JSON.parse( my_json_string );

alert( my_obj.prop == 'val' ); // Prompt is true, just as expected!

// Serialize the object into a JSON string
var my_other_json_string = JSON.stringify( my_obj );

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

More information
If you want to know more about JSON, the following link will definitely be useful to you:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn