Home >Web Front-end >JS Tutorial >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 <.>