JSON和Object Literal Notation有什么区别? [英] What is the difference between JSON and Object Literal Notation?
问题描述
有人能告诉我使用Object Literal Notation和 JSON对象定义的JavaScript对象之间的主要区别是什么?
Can someone tell me what is the main difference between a JavaScript object defined by using "Object Literal Notation" and JSON object?
根据一本JavaScript书,它说这是一个使用 Object Notation 定义的对象:
According to a JavaScript book it says this is an object defined by using Object Notation:
var anObject = {
property1 : true,
showMessage : function (msg) { alert(msg) }
};
为什么在这种情况下它不是JSON对象?仅因为它没有使用引号定义?
Why isn't it a JSON object in this case? Just because it is not defined by using quotation marks?
推荐答案
让我们首先澄清一下 JSON 实际上是。 JSON是一种文本,与语言无关的数据交换格式,非常类似于XML,CSV或YAML。
Lets clarify first what JSON actually is. JSON is a textual, language-indepedent data-exchange format, much like XML, CSV or YAML.
数据可以多种方式存储,但如果它应存储在文本文件中并且可由计算机读取,则需要遵循一些结构。 JSON是定义这种结构的众多格式之一。
Data can be stored in many ways, but if it should be stored in a text file and be readable by a computer, it needs to follow some structure. JSON is one of the many formats that define such a structure.
这些格式通常与语言无关,这意味着它们可以由Java,Python,JavaScript,PHP处理,你的名字。
Such formats are typically language-independent, meaning they can be processed by Java, Python, JavaScript, PHP, you name it.
相比之下, JavaScript 是一种编程语言。当然,JavaScript也提供了一种定义/描述数据的方法,但语法非常特定于JavaScript。
In contrast, JavaScript is a programming language. Of course JavaScript also provides a way to define/describe data, but the syntax is very specific to JavaScript.
作为反例,Python具有的概念元组,它们的语法是(x,y)
。 JavaScript没有这样的东西。
As a counter example, Python has the concept of tuples, their syntax is (x, y)
. JavaScript doesn't have something like this.
让我们看看JSON和JavaScript对象文字之间的语法差异。
Lets look at the syntactical differences between JSON and JavaScript object literals.
JSON具有以下语法约束:
JSON has the following syntactical constraints:
- 对象键必须是字符串(即用双引号括起来的字符序列
)。
- 值可以可以是:
- 一个字符串
- 一个数字
- 一个(JSON) )对象
- 数组
-
true
-
false
-
null
- Object keys must be strings (i.e. a character sequence enclosed in double quotes
"
). - The values can be either:
- a string
- a number
- an (JSON) object
- an array
true
false
null
在JavaScript中,对象文字可以有
In JavaScript, object literals can have
- 字符串文字,数字文字或标识符名称作为键(因为ES6,现在也可以计算密钥,这引入了另一种语法)。
- 值可以是任何有效的JavaScript表达式,包括函数定义和
undefined
。 - 重复键产生定义的指定结果(在松散模式下,后一个定义取代前者;在严格模式下,这是一个错误)。
知道这一点,只是看看语法,您的示例不是JSON,原因有两个:
Knowing that, just be looking at the syntax, your example is not JSON because of two reasons:
- 您的密钥不是字符串(文字) 。它们是标识符名称。
- 您不能将函数作为值分配给JSON对象(因为JSON没有为函数定义任何语法)。
但最重要的是,从头开始重复我的解释:您处于JavaScript环境中。您定义了一个JavaScript对象。如果有的话,JSON对象只能包含在字符串中:
But most importantly, to repeat my explanation from the beginning: You are in a JavaScript context. You define a JavaScript object. If any, a "JSON object" can only be contained in a string:
var obj = {foo: 42}; // creates a JavaScript object (this is *not* JSON) var json = '{"foo": 452}'; // creates a string containing JSON
也就是说,如果您正在编写JavaScript源代码,而不是处理字符串,你不是在处理JSON。也许你收到的数据是JSON(例如,通过ajax或从文件中读取),但是一旦你或你正在使用的库解析它,它就不再是JSON了。
That is, if you're writing JavaScript source code, and not dealing with a string, you're not dealing with JSON. Maybe you received the data as JSON (e.g., via ajax or reading from a file), but once you or a library you're using has parsed it, it's not JSON anymore.
仅因为对象文字和JSON看起来类似,这并不意味着您可以互换地命名它们。另请参阅没有JSON对象这样的东西。
Only because object literals and JSON look similar, it does not mean that you can name them interchangeably. See also There's no such thing as a "JSON Object".
这篇关于JSON和Object Literal Notation有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!