JSON和JavaScript对象有什么区别? [英] What are the differences between JSON and JavaScript object?

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

问题描述

我是JSON和JavaScript对象的新手。

I am new to JSON and JavaScript objects.


  • 有人可以解释一下JSON和JavaScript对象之间的区别吗?

  • 他们有什么用途?

  • 一个比另一个好吗?还是取决于具体情况?

  • 何时使用哪一种,在什么情况下?

  • 为什么首先创建JSON?它的主要目的是什么?

  • 有人可以举例说明何时应该使用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是什么:

First you should know what JSON is:


  • 语言不可知数据交换格式。

  • It is language agnostic data-interchange format.

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

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

例如,在JSON中,必须引用所有,而在对象文字中则不需要:

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

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

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

JSON必须引用引号,因为在JavaScript中(更准确地说,在ECMAScript 3rd。版本中),使用情况不允许保留字作为属性名称,例如:

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

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.

JSON中的数据类型也限制为以下值:

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


  • string

  • number

  • object

  • array

  • 一个文字为:


    • true

    • false

    • null

    • string
    • number
    • object
    • array
    • A literal as:
      • true
      • false
      • null

      字符串的语法更改。 必须双引号分隔,而在JavaScript中,您可以互换使用单引号或双引号。

      The grammar of Strings changes. They have to be delimited by double quotes, while in JavaScript, you can use single or double quotes interchangeably.

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

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

      The accepted JSON grammar of Numbers also 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.

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

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

      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.

      这篇关于JSON和JavaScript对象有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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