Using JSON to parse data into a usable object.
The quick popularity of JSON can be attributed to the reason that data could be parsed into an usable object very easily using JSON.
Parsing data using XML was bit difficult, as the data has to be parsed into the DOM document, making the extraction a bit of a cumbersome exercise
But now JSON object has two methods : stringify() and parse()
Example: JSON Syntax JSON Object.Give it a TRY! » Note:An an error is generated if the text passed into JSON.parse() is not a valid JSON.
The first argument is filter , which can be either a function or an array.
The second argument specifies an option for indenting the stringified JSON string.
The behavior is slightly different when the second argument is a function. The function receives two arguments : the property key and property value
Filters apply to all objects contained in the object which is to be serialized, so an array of multiple objects with properties will result in every object including the properties specified for filtering.
The third argument of JSON.stringify() is used to control indentation and white spaces.
The numerical values of the argument specifies the number of spaces ro indent each level.
If the indentation argument is a string , then the string is used as the indentation character for the JSONGive it a TRY! » Note:There is a limit of 10 characters, if a longer string is used then it is truncated to 10 characters only.
Other than JSON.stringify() , sometimes objects need custom JSON serialization, for that purpose the method toJSON() to return the proper JSON representation.
The method toJSON() can be used on any object to return a serialization value.
The method JSON.parse() accepts an additon argument, it is called on each key-value pair.
The function is also called as reviver function, the other function JSON.stringify() (filter) is called as a replacer
The format of both the function looks the same, they take two arguments : key and value and are supposed to return a value.