JSON 和 JavaScript 对象有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3975859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:58:09  来源:igfitidea点击:

What are the differences between JSON and JavaScript object?

javascriptjsonjavascript-objects

提问by Pheap

I am new to JSON and JavaScript objects.

我是 JSON 和 JavaScript 对象的新手。

  • Can someone please explain the differences between JSON and JavaScript object?
  • What are their uses?
  • Is one better than the other? Or does it depend on the situation?
  • When to use which one, in what situation?
  • Why was JSON created in the first place? What was its main purpose?
  • Can someone give examples of when one should use JSON rather than a JavaScript object and vice versa?
  • 有人可以解释一下 JSON 和 JavaScript 对象之间的区别吗?
  • 它们的用途是什么?
  • 这个比那个好吗?还是视情况而定?
  • 什么时候用,什么情况下?
  • 为什么首先创建 JSON?它的主要目的是什么?
  • 有人可以举例说明何时应该使用 JSON 而不是 JavaScript 对象,反之亦然?

回答by CMS

First you should know what JSON is:

首先你应该知道什么是 JSON:

  • It is language agnosticdata-interchange format.
  • 它是与语言无关的数据交换格式。

The syntax of JSON was inspired by the JavaScript Object Literal notation, but there are differences between them.

JSON 的语法受到 JavaScript Object Literal 表示法的启发,但它们之间存在差异。

For example, in JSON all keysmust be quoted, while in object literals this is not necessary:

例如,在 JSON 中,所有键都必须被引用,而在对象文字中,这不是必需的:

// JSON:
{ "foo": "bar" }

// Object literal:
var o = { foo: "bar" };

The quotes are mandatory on JSON because in JavaScript (more exactly in ECMAScript 3rd. Edition), the usage of reserved wordsas property names is disallowed, for example:

JSON 上的引号是强制性的,因为在 JavaScript(更确切地说是在 ECMAScript 3rd. Edition 中)中,不允许使用保留字作为属性名称,例如:

var o = { if: "foo" }; // SyntaxError in ES3

While, using a string literal as a property name (quoting the property name) gives no problems:

虽然,使用字符串文字作为属性名称(引用属性名称)没有问题:

var o = { "if": "foo" }; 

So for "compatibility"(and easy eval'ing maybe?) the quotes are mandatory.

因此,对于“兼容性”(可能是简单的评估?),引号是强制性的。

The data types in JSON are also restricted to the following values:

JSON 中的数据类型也仅限于以下值:

  • string
  • number
  • object
  • array
  • A literal as:
    • true
    • false
    • null
  • string
  • number
  • object
  • array
  • 文字如下:
    • true
    • false
    • null

The grammar of Stringschanges. They have tobe delimited by double quotes, while in JavaScript, you can use single or double quotes interchangeably.

Strings变化的语法。它们必须双引号分隔,而在 JavaScript 中,您可以交替使用单引号或双引号。

// Invalid JSON:
{ "foo": 'bar' }

The accepted JSON grammar of Numbersalso changes, in JavaScript you can use Hexadecimal Literals, for example 0xFF, or (the infamous) Octal Literals e.g. 010. In JSON you can use only Decimal Literals.

接受的 JSON 语法Numbers也发生了变化,在 JavaScript 中,您可以使用十六进制文字,例如0xFF,或(臭名昭著的)八进制文字,例如010。在 JSON 中,您只能使用十进制文字。

// Invalid JSON:
{ "foo": 0xFF }

There are some buggy implementations (Firefox 3.5+, IE8+, json2.js) where octal literals are wrongly allowed, e.g. JSON.parse('01')should produce a SyntaxError.

有一些错误的实现(Firefox 3.5+、IE8+、json2.js)其中八进制文字被错误地允许,例如JSON.parse('01')应该产生一个SyntaxError.

回答by Darin Dimitrov

JSON is a string representation of an object. It is an interoperable serialization format. It is not tied only to javascript. For example there are JSON serializers for .NET allowing you to serialize/deserialize .NET objects.

JSON 是对象的字符串表示形式。它是一种可互操作的序列化格式。它不仅与 javascript 相关。例如,有用于 .NET 的 JSON 序列化器,允许您序列化/反序列化 .NET 对象。

So it's just a format allowing you to convert from objects to string and back which is convenient if you want to transfer them over the wire.

所以它只是一种格式,允许您将对象转换为字符串并返回,如果您想通过网络传输它们,这很方便。

It is very close to javascript object representation and if you simply eval()a JSON string you will get the corresponding object.

它非常接近 javascript 对象表示,如果您只是eval()一个 JSON 字符串,您将获得相应的对象。

回答by Joey

JSON is a data interchange format, which just happens to look like a subset of YAML or JavaScript code you can execute and get an object back. A JavaScript object is just an object in JavaScript.

JSON 是一种数据交换格式,它恰好看起来像是 YAML 或 JavaScript 代码的子集,您可以执行并取回对象。JavaScript 对象只是 JavaScript 中的一个对象。

With JSON being a data interchange format you can exchange structured data in a textual form with it. It is pretty decoupled from JavaScript by now. JavaScript object allow you to create and work with structured data during the execution of a JavaScript program.

JSON 是一种数据交换格式,您可以与它交换文本形式的结构化数据。它现在已经与 JavaScript 完全解耦了。JavaScript 对象允许您在 JavaScript 程序执行期间创建和使用结构化数据。