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

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

{foo: 'bar', bar : 'baz'}

AFAIK, above is the object literal, json, and also the javascript object, right?

Does the object have literal and json the same?

How do you guys distinguish what it is?

+5
source share
6 answers

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

{"foo":"bar"}

JavaScript objects are part of the JavaScript language and are associated with things like a prototype.

javascript , new Object.create().

//object literal
var foo = {};

//equivalent
var foo = new Object();
+7

jsonString JSON:

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

javascriptObject javascript, :

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

json- javascript JSON.parse JSON.stringify.

+11

AFAIK, - , json, javascript, ?

. .

JSON, ( ). , (' ").

, , , ?

Context.

JSON () JavaScript. ( HTTP-).

- , ( .. ..).

+4

JSON JavaScript . , . JSON , - .

// 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 - ; JS
  • - JS
  • - , ,

JS JSON

var obj = JSON.parse( json );

JSON- ( )

var json = JSON.stringify( obj );
+4

JSON all, , .

JSON, :

{"foo": "bar", "bar": "baz"}
+2

Literal:

mozilla,

- , ({}).

Javascript:

mozilla,

JavaScript

JSON:

mozilla mozilla

JSON (JavaScript Object Notation) - . JavaScript, . JSON - , , , , . JavaScript , : - JavaScript JSON, JSON - JavaScript.

An object - javascript, .

An object literal - object.

JSON object literal, , .

(As a rule, there is a strictness that allows all languages ​​to use it, we cannot use it functionas a value, keyit should always be in double quotes (this is not necessary in the object literal))

0
source

All Articles