I want to directly convert a javascript object to string. I used following code.
var foo = {};
foo.test1 = test1;
foo.test2 = test2;
foo.test3 = test3;
var jsonObj = JSON.stringify(foo);
It works fine but it uses the json2 javascript library. However I need to do this in plain javascript without using any libraries. I know creating the json feed using passed parameters will work like this.
var jsonObj = "{\"test1\":\"" + test1+ "\",\"test2\":\"" + test2+ "\",\"test3\":\"" + test3+ "\"}";
However if the passed parameters(test1, test2 and test3) contains double quotes it will have issues.
What is the best approach to achieve this?
Thank you
JSON.stringify
natively. node.js does. – Andy E Jan 17 at 10:11