JSON 和 Object Literal Notation 有什么区别? [英] What is the difference between JSON and Object Literal Notation?

查看:41
本文介绍了JSON 和 Object Literal Notation 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我使用 Object Literal Notation 定义的 JavaScript 对象和 JSON 对象 之间的主要区别是什么?

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-independent 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
    • 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 by looking at the syntax, your example is not JSON because of two reasons:

      1. 您的键不是字符串(文字).它们是标识符名称.
      2. 您不能将函数作为值分配给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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆