JSON stringify missing in jQuery 1.4.1?

Obviously, jQuery has the ability to decode a given object or string into a JSON object. However, I have a JS object that I need to send POST to the server, and I do not find in jQuery a utility that wraps the JSON.stringify () function. This feature is found in Chrome, Safari 4, FF3.6, and IE8, but not found in earlier browsers. I can use it natively in browsers that support it, but otherwise I have to revert to using Crockford's JSON scripts.

Is there a built-in jQuery function that handles JSON encoding and decoding, replacing Crockford scripts?

+74
json jquery decode encode
Feb 17 '10 at 0:13
source share
6 answers

You can check this out: http://www.json.org/js.html

+30
Feb 17 '10 at 0:15
source share
— -

You can use the "Closure Library" (Google) to create a cross-browser JSON encoder / decoder.

Just go to http://closure-compiler.appspot.com/

and paste the following into the text box, then click Compile:

// ==ClosureCompiler== // @compilation_level ADVANCED_OPTIMIZATIONS // @output_file_name default.js // @use_closure_library true // ==/ClosureCompiler== goog.require('goog.json'); if (!window['JSON']) window['JSON']={}; if (typeof window['JSON']['stringify'] !== 'function') window['JSON']['stringify']=goog.json.serialize; if (typeof window['JSON']['parse'] !== 'function') window['JSON']['parse']=goog.json.parse; 
+26
Oct 17 '10 at 20:57
source share

jQuery can decode JSON strings natively jQuery.parseJSON() .

However, for coding, I only know the plugin: jquery-json

+14
Feb 17 '10 at 0:19
source share

jQuery does not need this function internally and therefore does not provide a convenient method for this.

JSON.stringify () is the standard and recommended way to encode an object into a string representation of the JSON of this object. This is a native JSON object method in many browsers, and it is recommended to use json2.js (https://github.com/douglascrockford/JSON-js) to provide a reserve.

+3
Mar 13 2018-12-12T00: 00Z
source share

To build a response to stewe, a closure compiler with the add-on turned on gives you a script that pollutes the global namespace with a bunch of one letter variable. So, I just wrapped it with an anonymous function call as follows:

(function() { function g(a){var b=typeof a;if("object"==b)if(a){if(a instanceof Array)return"array";if(a instanceof Object)return b;var c=Object.prototype.toString.call(a);if("[object Window]"==c)return"object";if("[object Array]"==c||"number"==typeof a.length&&"undefined"!=typeof a.splice&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("splice"))return"array";if("[object Function]"==c||"undefined"!=typeof a.call&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("call"))return"function"}else return"null"; else if("function"==b&&"undefined"==typeof a.call)return"object";return b};function h(a){a=""+a;if(/^\s*$/.test(a)?0:/^[\],:{}\s\u2028\u2029]*$/.test(a.replace(/\\["\\\/bfnrtu]/g,"@").replace(/"[^"\\\n\r\u2028\u2029\x00-\x08\x10-\x1f\x80-\x9f]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:[\s\u2028\u2029]*\[)+/g,"")))try{return eval("("+a+")")}catch(b){}throw Error("Invalid JSON string: "+a);}function i(a,b){var c=[];j(new k(b),a,c);return c.join("")}function k(a){this.a=a} function j(a,b,c){switch(typeof b){case "string":l(b,c);break;case "number":c.push(isFinite(b)&&!isNaN(b)?b:"null");break;case "boolean":c.push(b);break;case "undefined":c.push("null");break;case "object":if(null==b){c.push("null");break}if("array"==g(b)){var f=b.length;c.push("[");for(var d="",e=0;e<f;e++)c.push(d),d=b[e],j(a,aa?aacall(b,""+e,d):d,c),d=",";c.push("]");break}c.push("{");f="";for(e in b)Object.prototype.hasOwnProperty.call(b,e)&&(d=b[e],"function"!=typeof d&&(c.push(f),l(e,c),c.push(":"), j(a,aa?aacall(b,e,d):d,c),f=","));c.push("}");break;case "function":break;default:throw Error("Unknown type: "+typeof b);}}var m={'"':'\\"',"\\":"\\\\","/":"\\/","\u0008":"\\b","\u000c":"\\f","\n":"\\n","\r":"\\r","\t":"\\t","\x0B":"\\u000b"},n=/\uffff/.test("\uffff")?/[\\\"\x00-\x1f\x7f-\uffff]/g:/[\\\"\x00-\x1f\x7f-\xff]/g; function l(a,b){b.push('"',a.replace(n,function(a){if(a in m)return m[a];var b=a.charCodeAt(0),d="\\u";16>b?d+="000":256>b?d+="00":4096>b&&(d+="0");return m[a]=d+b.toString(16)}),'"')};window.JSON||(window.JSON={});"function"!==typeof window.JSON.stringify&&(window.JSON.stringify=i);"function"!==typeof window.JSON.parse&&(window.JSON.parse=h); })();

Now you can call:

var JSONString = JSON.stringify({name: 'value'});

+2
Mar 30 '12 at 18:25
source share

Often when using jQuery, the JSON.stringify () function is not required. Take, for example, the usual case of using ajax to send javascript data to the server, jquery has built-in functions to handle this: (examples from http://api.jquery.com/category/ajax/ )

 $.post("test.php", { name: "John", time: "2pm" } ); $.post("test.php", { 'choices[]': ["Jon", "Susan"] }); $.getJSON("test.js", { name: "John", time: "2pm" }, function(json) { alert("JSON Data: " + json.users[3].name); }); 

In all the examples above, the javascript data sent is automatically serialized by jQuery.

Serialization in these cases is not the same as JSON.Stringify (), instead the data is serialized to the html query string (see http://en.wikipedia.org/wiki/Query_string#Structure ).

However, this form of serialization is suitable for most (but not all) applications.

+1
Mar 14 '12 at 19:20
source share



All Articles