Skip to content

Instantly share code, notes, and snippets.

@mikeflynn
Created September 11, 2010 06:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeflynn/574908 to your computer and use it in GitHub Desktop.
Save mikeflynn/574908 to your computer and use it in GitHub Desktop.
function toJSON(object) {
var json = new Array();
for(var index in object) {
type = typeof(object[index]);
if(type == 'object'){
json.push('"'+index+'":' + toJSON(object[index]));
} else if(type == 'string') {
json.push('"'+index+'":' + '"'+object[index].replace(/\"/g,'\\"')+'"');
} else if(type == 'boolean' || type == 'number') {
json.push('"'+index+'":' +object[index]);
}
}
string = '{' + json.join(',') + '}';
return string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment