views:

136

answers:

3

Hi folks,

i am newbie to JSON & hence i am not sure what $.toJSON(params) means?

Please explain what this does?

TIA

A: 

It passes the variable params as an argument to the method named toJSON attached to the object stored in the (unhelpfully named) variable $.

Based on the name, it probably converts the contents of the params variable to a String formatted according to the JSON specification.

David Dorward
+6  A: 

It could be this jQuery plugin

var myObj = {};
myObj.propA = "a";
myObj.propB = "b";
myObj.propC = "c";
var jsonString = $.toJSON(myObj); // same as jQuery.toJSON(myObj)
// output:  '{ "propA" : "a", "propB" : "b", "propC" : "c" }'
rchern
Most likely is this plugin, but why you would use that plugin over `json2.js` I don't know....
gnarf
A: 

See: http://www.json.org/js.html

A JSON stringifier goes in the opposite direction, converting JavaScript data structures into JSON text. JSON does not support cyclic data structures, so be careful to not give cyclical structures to the JSON stringifier.

var myJSONText = JSON.stringify(myObject, replacer);

If the stringify method sees an object that contains a toJSON method, it calls that method, and stringifies the value returned. This allows an object to determine its own JSON representation.

The stringifier method can take an optional array of strings. These strings are used to select the properties that will be included in the JSON text.

The stringifier method can take an optional replacer function. It will be called after the toJSON method (if there is one) on each of the values in the structure. It will be passed each key and value as parameters, and this will be bound to object holding the key. The value returned will be stringified.

So if you have a $.toJSON() method, it could be a badly implemented function to "stringify", or it could be a method that returns the "JSON Representation" of $

gnarf