What is the difference between a JS object literal and a JSON string?

后端 未结 6 1714
灰色年华
灰色年华 2021-02-04 19:47

I have confusion about what exactly people mean by Object Literals, JSON, JavaScript Objects, to me they seem similar:

{foo: \'bar\', bar : \'baz\'}
相关标签:
6条回答
  • 2021-02-04 20:03

    According to the specification, in JSON all strings, whether they are values or keywords, should be surrounded by double quotes.

    Your example would be a valid JSON string if it contains the following:

    {"foo": "bar", "bar": "baz"}
    
    0 讨论(0)
  • 2021-02-04 20:05

    The variable jsonString contains a JSON string:

    var jsonString = '{"foo": "bar", "bar" : "baz"}'
    

    The variable javascriptObject contains a javascript object, initialized using an object literal:

    var javascriptObject =  {foo: 'bar', bar : 'baz'}
    

    You can convert a json string to a javascript object with JSON.parse, and back again with JSON.stringify.

    0 讨论(0)
  • 2021-02-04 20:06

    Object Literal:

    Referencing mozilla ,

    An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}).

    Javascript Object:

    Referencing mozilla,

    In JavaScript, an object is a standalone entity, with properties and type

    JSON:

    Referencing mozilla and mozilla

    JSON (JavaScript Object Notation) is a data-interchange format. It closely resembles a subset of JavaScript syntax, although it is not a strict subset.JSON is a syntax for serializing objects, arrays, numbers, strings, booleans, and null. It is based upon JavaScript syntax but is distinct from it: some JavaScript is not JSON, and some JSON is not JavaScript.

    In loose words,

    An object is a javascript variable which can have properties and type.

    An object literal is a way to assign properties and associated values to object.

    JSON is a more strict object literal used for data interchange which is wrapped as string.

    (Usually, strictness is there to allow all languages to use it, we cannot use function as value, key should always be in double quotes (In object literal its not compulsory))

    0 讨论(0)
  • 2021-02-04 20:12

    AFAIK, above is object literal, json as well as javascript object, isn't it?

    It is an object literal. It creates an object.

    It is not JSON, as it doesn't conform to the syntax (which is a subset of object literal notation). The keys are not quoted and the wrong kind of quote marks (' instead of ") are used around the values.

    How do you guys differentiate which is what ?

    Context.

    JSON doesn't usually appear (embedded) in the middle of JavaScript programs. It is a data format and usually appears as whole files (or HTTP responses).

    When something expects an object it could get one from an object literal or from a variable (or a return value from a function call, etc, etc).

    0 讨论(0)
  • 2021-02-04 20:15

    JSON originates from the object literal notation of JavaScript and itself is a string. That explains the similarity, when just looking at it. Today JSON is used as a general means of serializing all kinds of data, before submitting it over some network or storing it.

    // this is a JSON variable
    var json = '{"foo": "bar", "bar" : "baz"}';
    
    // obj is a JavaScript obj, defined by the object literal on the right hand side
    var obj = {foo: 'bar', bar : 'baz'};
    
    • JSON - serialized object; similar syntax as defining an object in JS
    • Object literal - shorthand syntax to define an object in JS
    • Object - the result of a definition by, e.g., an object literal

    In JS you can convert a JSON string into an object by using

    var obj = JSON.parse( json );
    

    and get the JSON representation of an object (excluding attached functions) by

    var json = JSON.stringify( obj );
    
    0 讨论(0)
  • 2021-02-04 20:19

    JSON is a just a data format, like XML. True JSON should have the keys surrounded by double quotes, like so:

    {"foo":"bar"}
    

    JavaScript Objects are part of the JavaScript language, and have associated things such as a prototype.

    Object literals is creating a javascript object in place with brackets as opposed to using the new keyword, or Object.create().

    //object literal
    var foo = {};
    
    //equivalent
    var foo = new Object();
    
    0 讨论(0)
提交回复
热议问题