Javascript object Vs JSON

后端 未结 5 1105
故里飘歌
故里飘歌 2020-11-22 08:04

I want to understand the basic differences clearly between Javascript object and JSON string.

Let\'s say I create the following JS variable:

var test         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 08:21

    Problems solved by JSON

    Let's say you want to exchange regular JavaScript objects between two computers, and you set two rules:

    • The transmitted data must be a regular string.
    • Only attributes can be exchanged, methods are not transmitted.

    Now you create two objects on the first host:

    var obj1 = { one: 1,"two":2,"three":3 }; // your example
    var obj2 = { one: obj1.one, two: 2, three: obj1.one + obj1.two };
    

    How can you convert those objects into strings for transmission to the second host?

    • For the first object, you could send this string obtained form the literal definition '{ one: 1,"two":2,"three":3 }', but actually you can't read the literal in the script portion of the document (at least not easily). So obj1 and obj2 must actually be processed the same way.
    • You need to enumerate all attributes and their value, and build a string similar to the object literal.

    JSON has been created as a solution to the needs just discussed: It is a set of rules to create a string equivalent to an object by listing all attributes and values (methods are ignored).

    JSON normalizes the use of double-quotes for attribute names and values.

    Remember that JSON is a set of rules only (a standard).

    How many JSON objects are created?

    Only one, it is automatically created by the JS engine.

    Modern JavaScript engines found in browsers have a native object, also named JSON. This JSON object is able to:

    • Decode a string built using JSON standard, using JSON.parse(string). The result is a regular JS object with attributes and values found in the JSON string.

    • Encode attributes / values of a regular JS object using JSON.stringify(). The result is a string compliant with the JSON set of rules.

    The (single) JSON object is similar to a codec, it's function is to encode and decode.

    Note that:

    • JSON.parse() doesn't create a JSON object, it creates a regular JS object, there is no difference between an object created using an object literal and an object created by JSON.parse() from a JSON-compliant string.

    • There is only one JSON object, which is used for all conversions.

    Going back to the questions:

    • Q1: The use of single of double quotes is allowed for object literals. Note that the quotes are used optionally for attributes names, and are mandatory for string values. The object literal itself is not surrounded by quotes.

    • Q2: Objects created from literals and using JSON.parse() are strictly the same. These two objects are equivalent after creation:

      var obj1 = { one: 1, "two": 2, "three": 3 };
      var obj2 = JSON.parse('{ "one": "1", "two": "2", "three": "3" }');

    • Q3: On modern browsers JSON.parse() is used to create a JS object from a JSON-compliant string. (jQuery has also an equivalent method that can be used for all browsers).

提交回复
热议问题