Created
November 19, 2014 14:54
-
-
Save shaunwallace/8a827599c9d41becbf80 to your computer and use it in GitHub Desktop.
JSON.stringify()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JSON.stringify({foo : true, bar : false}); // returns "{"foo":true,"bar":false}" | |
JSON.stringify([1, "false", false]); // returns "[1,"false",false]" note the last false is not surrounded by quotes | |
JSON.stringify({ a: 2 }, null, " "); // returns '{\n "a": 2\n}' | |
JSON.stringify({ a: 2 }, null, 4); // returns '{\n "a": 2\n}' | |
function replacer(k,v) { | |
if( typeof v === "string") return; | |
if( typeof v === "number") return 2*v; | |
return v; | |
} | |
JSON.stringify({ a: 2, b : "Some String" }, replacer, 4); // returns "{\n "a": 4}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment