Last active
May 4, 2018 07:44
-
-
Save dhwang/b20f30e66fc58ae041935f2b10a11e48 to your computer and use it in GitHub Desktop.
This file contains 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 JSONstringify( obj ) { | |
var val, results = []; | |
results.push('{') | |
for(var key in obj) { | |
val = obj[key]; | |
results.push('"'+ key + '": ') | |
if (typeof(val) === 'number') { | |
results.push(val) | |
} else | |
if (typeof(val) === 'string') { | |
results.push( '"' + val + '"') | |
} else | |
if (typeof(val) === 'object') { | |
if (val instanceof Array) { | |
results.push('[') | |
for(var i=0; i < val.length; i++) { | |
if (typeof(val[i]) === 'object') { | |
results.push(JSONstringify(val[i])) | |
} else { | |
results.push(val[i]) | |
} | |
results.push(', ') | |
} | |
results.pop(); | |
results.push("]") | |
} else { | |
results.push(JSONstringify(val)); | |
} | |
} | |
results.push(', '); | |
} | |
results.pop(); | |
results.push('}') | |
return results.join(''); | |
} | |
console.log(JSONstringify( | |
{ a: { | |
b: 1, | |
c: "string hello", | |
d: [1,"2", {e:"one"}] | |
}, | |
f:"1234" | |
} | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment