首页 > 代码库 > JSON.stringify

JSON.stringify

var contact = new Object(); contact.firstname = "Jesper"; contact.surname = "Aaberg"; contact.phone = ["555-0100", "555-0120"];  var memberfilter = new Array(); memberfilter[0] = "surname"; memberfilter[1] = "phone"; var jsonText = JSON.stringify(contact, memberfilter, "\t"); document.write(jsonText); // Output:  // { "surname": "Aaberg", "phone": [ "555-0100", "555-0120" ] }
var continents = new Array(); continents[0] = "Europe"; continents[1] = "Asia"; continents[2] = "Australia"; 

var jsonText = JSON.stringify(continents, replaceToUpper); function replaceToUpper(key, value) { return value.toString().toUpperCase(); } document.write(jsonText); //Output: // "EUROPE,ASIA,AUSTRALIA"
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); document.write(jsonText);  // Output: //{"firstname":"JESPER","surname":"AABERG","phone":["555-0100","555-0120"]}