Created
September 3, 2013 09:51
-
-
Save callblueday/6421842 to your computer and use it in GitHub Desktop.
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
function jsonToString(obj){ | |
var THIS = this; | |
switch(typeof(obj)){ | |
case 'string': | |
return '"' + obj.replace(/(["\\])/g, '\\$1') + '"'; | |
case 'array': | |
return '[' + obj.map(THIS.jsonToString).join(',') + ']'; | |
case 'object': | |
if(obj instanceof Array){ | |
var strArr = []; | |
var len = obj.length; | |
for(var i=0; i<len; i++){ | |
strArr.push(THIS.jsonToString(obj[i])); | |
} | |
return '[' + strArr.join(',') + ']'; | |
}else if(obj==null){ | |
return 'null'; | |
}else{ | |
var string = []; | |
for (var property in obj) string.push(THIS.jsonToString(property) + ':' + THIS.jsonToString(obj[property])); | |
return '{' + string.join(',') + '}'; | |
} | |
case 'number': | |
return obj; | |
case false: | |
return obj; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment