Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

var JSONobj = new JSON({'a':'b'})
like image 979
aaltruista Avatar asked Jun 19 '11 20:06

aaltruista


People also ask

How do you create a JSON object?

To create an object we need to use opening and closing curly braces {} and then inside of that we'll put all of the key value pairs that make up our object. Every single property inside the JSON is a key value pair. The key must be surrounded by double "" quotes followed by a colon : and then the value for that key.

Which type of data structure is most suitable for JSON format?

JSON is an ideal format for larger data that have a hierarchical structured relationship. The structure of a JSON object is as follows: The data are in name/value pairs. Data objects are separated by commas.

How JSON file is used in jQuery?

To load JSON data using jQuery, use the getJSON() and ajax() method. The jQuery. getJSON( ) method loads JSON data from the server using a GET HTTP request. data − This optional parameter represents key/value pairs that will be sent to the server.

What is the proper format of JSON?

JSON Syntax RulesData is in name/value pairs. Data is separated by commas. Curly braces hold objects. Square brackets hold arrays.


1 Answers

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse 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 language independent but uses conventions that are familiar to programmers of the C-family of languages...These properties make JSON an ideal data-interchange language.

source

JSON is a subset of the object literal notation of JavaScript. Since JSON is a subset of JavaScript, it can be used in the language with no muss or fuss.

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

source

However to parse JSON from an external source or serialize JSON objects from your own code, you'll need a library such as JSON-js as Javascript/ECMAScript doesn't currently support this, although:

It is expected that native JSON support will be included in the next ECMAScript standard.

like image 186
Andy Avatar answered Sep 25 '22 19:09

Andy