What is the difference between JSON and Object Lit

2018-12-30 22:22发布

Can someone tell me what is the main difference between a JavaScript object defined by using "Object Literal Notation" and JSON object?

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) }
};

Why isn't it a JSON object in this case? Just because it is not defined by using quotation marks?

8条回答
笑指拈花
2楼-- · 2018-12-30 23:15

For the ones who still think that RFC are more important than blogs and opinion based misconceptions, let's try to answer clarifying some points. I'm not going to repeat all the correct differences already mentioned in previous answers, here I'm just trying adding value summarizing some crucial part rfc7159

Extracts from https://tools.ietf.org/html/rfc7159

  1. JavaScript Object Notation (JSON) is a text format for the serialization of structured data. It is derived from the object literals of JavaScript, as defined in the ECMAScript Programming Language Standard, Third Edition [ECMA-262].
  2. JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays).
  3. An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.
  4. begin-object = ws %x7B ws ; { left curly bracket
  5. end-object = ws %x7D ws ; } right curly bracket
  6. A JSON value MUST be an object, array, number, or string, or one of the following three literal names: false null true
  7. An object structure is represented as a pair of curly brackets
  8. The names within an object SHOULD be unique. object = begin-object [ member *( value-separator member ) ] end-object
  9. An object whose names are all unique is interoperable in the sense that all software implementations receiving that object will agree on the name-value mappings. When the names within an object are not unique, the behavior of software that receives such an object is unpredictable.
  10. Examples (from page 12 of RFC)

    This is a JSON object:

          {
            "Image": {
                "Width":  800,
                "Height": 600,
                "Title":  "View from 15th Floor",
                "Thumbnail": {
                    "Url":    "http://www.example.com/image/481989943",
                    "Height": 125,
                    "Width":  100
                },
                "Animated" : false,
                "IDs": [116, 943, 234, 38793]
              }
          }
    

    Its Image member is an object whose Thumbnail member is an object and whose IDs member is an array of numbers.

There is really no such thing as a "JSON Object".

Really?

查看更多
孤独寂梦人
3楼-- · 2018-12-30 23:15

First you should know what JSON is:

It is language agnostic data-interchange format. The syntax of JSON was inspired by the JavaScript Object Literal notation, but there are differences between them.

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" }; 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.

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

string number object array A literal as: true false null 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' } 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 }

查看更多
登录 后发表回答