JSON.stringify test
HTML
<div id="results"></div>
CSS
.header {
font-family: Arial;
background-color: MidnightBlue;
color: white;
margin-top: 2em;
padding: .25em;
border-top: 1pt solid SteelBlue;
border-bottom: 1pt solid SteelBlue;
}
JavaScript
var person = {
name: "Jim Cowart",
/*location: {
city: {
name: "Chattanooga",
population: 167674
},
state: {
name: "Tennessee",
abbreviation: "TN",
population: 6403000
}
},*/
company: "appendTo", /*,
toJSON: function () {
return {
booyah : this.name + ' , employer: ' + this.company
};
}*/
test: function(){
var i = 0;
}
};
function writeToDom(title, content) {
$("#results").append("<div class='header'>" + title + ":</div><div><pre>" + content + "</pre></div>");
}
var replacerFn = function(key, value) {
//alert(key + ", " + value);
if(!key || key === 'name' || key === 'company') {
return value;
}
if(key === 'test'){
var val = "" + value + "";
alert(JSON.stringify(val));
return JSON.stringify(val);
}
return; // returning undefined omits the key from being serialized
}
// The value returned from toJSON above is what
// will actually get stringified:
writeToDom('Result', JSON.stringify(person, null, 4));
var str = JSON.stringify(person, replacerFn, 4);
writeToDom('Result of ReplacrFunc', str);
var json = JSON.parse(str, function(k,v) {
if(k === 'test') {
// return v.substr(2, v.length -3);
console.log(JSON.parse(v));
return JSON.parse(v);
}
else{
return v;
}
});
//writeToDom(' json ', json);
//console.log('json' , json);
console.log( 'AAA STR' + JSON.stringify(person, null, 10) );
/* produces:
"{
"booyah": "Jim Cowart , employer: appendTo"
}"
*/