Monday 30 September 2019

javascript - Convert JS object to JSON string



If I defined an object in JS with:




var j={"name":"binchen"};


How can I convert the object to JSON? The output string should be:



'{"name":"binchen"}'

Answer



All current browsers have native JSON support built in. So as long as you're not dealing with prehistoric browsers like IE6/7 you can do it just as easily as that:




var j={"name":"binchen"};
JSON.stringify(j); // '{"name":"binchen"}'

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...