How can I convert a JavaScript object into a string?
Example:
var o = {a:1, b:2}
console.log(o)
console.log('Item: ' + o)
Output:
Object { a=1, b=2} // very nice readable output :)
Item: [object Object] // no idea what's inside :(
How can I convert a JavaScript object into a string?
Example:
var o = {a:1, b:2}
console.log(o)
console.log('Item: ' + o)
Output:
Object { a=1, b=2} // very nice readable output :)
Item: [object Object] // no idea what's inside :(
If you only care about strings, objects, and arrays:
If you are using the Dojo javascript framework then there is already a build in function to do this: dojo.toJson() which would be used like so.
which will return a string. If you want to convert the object to json data then add a second parameter of true.
http://dojotoolkit.org/reference-guide/dojo/toJson.html#dojo-tojson
example to use:
your_object1.txt:
your_object2.txt:
If you wont aplay join() to Object.
I had need to make a more configurable version of
JSON.stringify
as I had to add comments and know the JSON path: