I know jQuery automatically turns JSON into string when sending AJAX POST requests. Is it possible to convert JSON to string the same way without sending it via AJAX?
2 Answers
Use the JSON stringifier at JSON.org
BTW I don't think jQuery is converting JSON to a string. I think the conversion of the JSON to a POST message is done internally by the browser as part of the XmlHttpRequest.... but could be wrong... haven't really looked at the code in jQuery for few versions.
2 Comments
jQuery.parseJSON() function.Most browsers have a native JSON object these days, which includes parse and stringify methods. So just try JSON.stringify({}) and see if you get "{}". You can even pass in parameters to filter out keys or to do pretty-printing, e.g. JSON.stringify({a:1,b:2}, null, 2) puts a newline and 2 spaces in front of each key.
JSON.stringify({a:1,b:2}, null, 2)
gives
"{\n \"a\": 1,\n \"b\": 2\n}"
which prints as
{
"a": 1,
"b": 2
}
See http://www.javascriptkit.com/jsref/json.shtml for more info.