A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://webplatform.github.io/docs/javascript/Date/toJSON below:

toJSON · WebPlatform Docs

toJSON Summary

Used by the JSON.stringify method to enable the transformation of an object’s data for JavaScript Object Notation (JSON) serialization.

Syntax
objectname.toJSON()
objectname
Required. An object for which JSON serialization is wanted.
Examples

The following example uses the toJSON method to serialize string member values in uppercase. The toJSON method is called when JSON.stringify is called.

var contact = new Object();
 contact.firstname = "Jesper";
 contact.surname = "Aaberg";
 contact.phone = ["555-0100", "555-0120"];

 contact.toJSON = function(key)
  {
     var replacement = new Object();
     for (var val in this)
     {
         if (typeof (this[val]) === 'string')
             replacement[val] = this[val].toUpperCase();
         else
             replacement[val] = this[val]
     }
     return replacement;
 };

 var jsonText = JSON.stringify(contact);

 

The following example illustrates how to use the toJSON method that is a built-in member of the Date object.

var dt = new Date('8/24/2009');
 dt.setUTCHours(7, 30, 0);
 var jsonText = JSON.stringify(dt);

 
Remarks

The toJSON method is used by the JSON.stringify function.JSON.stringify serializes a JavaScript value into JSON text. If a toJSON method is provided to JSON.stringify , the toJSON method is called when JSON.stringify is called.

The toJSON method is a built-in member of the Date JavaScript object. It returns an ISO-formatted date string for the UTC time zone (denoted by the suffix Z).

You can override the toJSON method for the Date type, or define a toJSON method for other object types to achieve transformation of data for the specific object type before JSON serialization.

See also Other articles Attributions

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4