Please enable JavaScript.
Coggle requires JavaScript to display documents.
JSON methods, toJSON - Coggle Diagram
JSON methods, toJSON
-
-
-
-
-
Using reviver
Some type data have mistake when to turn back into JavaScript object. like Date, so we need reviver
let meetup = JSON.parse(str, function(key, value) { if (key == 'date') return new Date(value); return value; });
-
Custom “toJSON”
an object may provide method toJSON for to-JSON conversion. JSON.stringify automatically calls it if available
let room = { number: 23, toJSON() { return this.number; } };
toJSON is used both for the direct call JSON.stringify(room) and when room is nested in another encoded object., (it help flat json when object nested)
JSON.parse
syntax
let value = JSON.parse(str, [reviver]);
-
reviver: Optional function(key,value) that will be called for each (key, value) pair and can transform the value.