


In-depth Understanding of JavaScript Series (9) There is no such thing as a 'JSON object'! _javascript skills
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 <.>
var foo = '{ "prop": "val" }';
// This is an object literal
var bar = { "prop": "val" };
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:
// 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:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
