首页 > 代码库 > 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"]}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。