What is the best way to create a JSON object in jQuery?

What is the best way to create a JSON object in jQuery (without using a parser or AJAX)?

var JSONobj = new JSON({'a':'b'}) 
+6
source share
4 answers

JSON (JavaScript object notation) is an easy data exchange format. It’s easy for a person to read and write. Easy for machines to analyze and generate. It is based on a subset of the JavaScript programming language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely independent of the language, but uses conventions that are familiar to the programmers of the C-family of languages ​​... These properties make JSON an ideal data exchange language.

a source

JSON is a subset of JavaScript object literary notation. Since JSON is a subset of JavaScript, it can be used in a language without garbage or fuss.

 var myJSONObject = {"bindings": [ {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}, {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"}, {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"} ] }; 

a source

However, to parse JSON from an external source or to serialize JSON objects from your own code, you will need a library such as JSON-js since Javascript / ECMAScript does not currently support this, though:

Native JSON support is expected to be included in the next ECMAScript standard.

+6
source

JSON is a serialized representation of an object. This is just a string. To create a JSON representation from a JavaScript object, use JSON.stringify .

 var myObject = { hello: "world", foo: [ "bar", "baz", 42 ] }; JSON.stringify(myObject); // "{"hello":"world","foo":["bar","baz",42]}" 
+4
source

You should simply use the object literal syntax:

 var JSONobj = {'a':'b'}; 
+3
source

I'm not sure what you are trying to do, but if you just want to create an object, create it ...

 var myObj = { a : "b" }; 
+1
source

Source: https://habr.com/ru/post/890877/


All Articles