Lets clarify first what JSON actually is.(首先让我们澄清一下什么是JSON 。)
JSON is a textual , language-indepedent data-exchange format, much like XML, CSV or YAML.(JSON是一种文本形式的,不依赖语言的数据交换格式,非常类似于XML,CSV或YAML。)
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.(JSON是定义这种结构的多种格式之一。)
Such formats are typically language-independent, meaning they can be processed by Java, Python, JavaScript, PHP, you name it.(这样的格式通常与语言无关,这意味着它们可以通过Java,Python,JavaScript,PHP进行处理,只要您命名即可。)
In contrast, JavaScript is a programming language.(相反, JavaScript是一种编程语言。)
Of course JavaScript also provides a way to define/describe data, but the syntax is very specific to JavaScript.(当然,JavaScript还提供了一种定义/描述数据的方法,但是语法非常特定于JavaScript。)
As a counter example, Python has the concept of tuples , their syntax is (x, y)
.(作为反例,Python具有元组的概念,其语法为(x, y)
。)
JavaScript doesn't have something like this.(JavaScript没有这样的东西。)
Lets look at the syntactical differences between JSON and JavaScript object literals.(让我们看一下JSON和JavaScript对象文字之间的语法差异。)
JSON has the following syntactical constraints:(JSON具有以下语法约束:)
- Object keys must be strings (ie a character sequence enclosed in double quotes
"
).(对象键必须是字符串 (即用双引号"
括起来的字符序列)。)
- The values can be either:(值可以是:)
- a string(一个字符串)
- a number(一个号码)
- an (JSON) object((JSON)对象)
- an array(数组)
-
true
-
false
-
null
- Duplicate keys (
{"foo":"bar","foo":"baz"}
) produce undefined, implementation-specific results;(重复键( {"foo":"bar","foo":"baz"}
)产生未定义的,特定于实现的结果;) the JSON specification specifically does not define their semantics(JSON规范没有明确定义其语义)
In JavaScript, object literals can have(在JavaScript中,对象文字可以具有)
- String literals, number literals or identifier names as keys (since ES6, keys can now also be computed, which introduces yet another syntax).(字符串文字,数字文字或标识符名称作为键(自ES6开始,现在还可以计算键,这引入了另一种语法)。)
- The values can be any valid JavaScript expression, including function definitions and
undefined
.(值可以是任何有效的JavaScript表达式,包括函数定义和undefined
。)
- Duplicate keys produce defined, specified results (in loose mode, the latter definition replaces the former; in strict mode, it's an error).(重复的键会产生已定义的指定结果(在宽松模式下,后一个定义将替换前一个;在严格模式下,这是一个错误)。)
Knowing that, just be looking at the syntax , your example is not JSON because of two reasons:(知道了,仅查看语法 ,您的示例就不是JSON,原因有两个:)
- Your keys are not strings (literals).(您的密钥不是字符串(文字)。) They are identifier names .(它们是标识符名称 。)
- You cannot assign a function as a value to a "JSON object" (because JSON doesn't define any syntax for functions).(您不能将函数作为值分配给“ JSON对象”(因为JSON没有为函数定义任何语法)。)
But most importantly, to repeat my explanation from the beginning: You are in a JavaScript context.(但最重要的是,从头开始重复我的解释:您处于JavaScript上下文中。)
You define a JavaScript object.(您定义一个JavaScript对象。) If any, a "JSON object" can only be contained in a string:(如果有的话,“ JSON对象”只能包含在字符串中:)
var obj = {foo: 42}; // creates a JavaScript object (this is *not* JSON)
var json = '{"foo": 452}'; // creates a string containing JSON
That is, if you're writing JavaScript source code, and not dealing with a string , you're not dealing with JSON.(也就是说,如果您正在编写JavaScript源代码,并且没有处理字符串 ,那么您就不会处理JSON。)
Maybe you received the data as JSON (eg, 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形式接收了数据(例如,通过ajax或从文件中读取),但是一旦您或所使用的库将其解析后,它便不再是JSON。)
Only because object literals and JSON look similar , it does not mean that you can name them interchangeably.(仅因为对象文字和JSON看起来相似 ,并不意味着您可以互换命名它们。)
See also There's no such thing as a "JSON Object" .(另请参阅没有“ JSON对象”之类的东西 。)