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

后端 未结 6 1734
灰色年华
灰色年华 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: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.

提交回复
热议问题