Skip to content

Instantly share code, notes, and snippets.

@dhwang
Last active May 4, 2018 07:44
Show Gist options
  • Save dhwang/b20f30e66fc58ae041935f2b10a11e48 to your computer and use it in GitHub Desktop.
Save dhwang/b20f30e66fc58ae041935f2b10a11e48 to your computer and use it in GitHub Desktop.
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