質問

明らかにjQueryはJSONオブジェクトに指定されたオブジェクトまたは文字列をデコードする能力を有します。しかし、私は、サーバーにポストバックする必要があると私はJSON.stringify()関数をラップjQueryのには有用性が見出されなかっJSオブジェクトを持っています。この関数は、クローム、サファリ4、FF3.6、およびIE8に発見されたが、それ以前のブラウザでは見られません。私はそれをサポートするブラウザでネイティブにそれを使用することができますが、それ以外はクロックフォードのJSONスクリプトを使用してにフォールバックするように強制しています。

あり、いくつかの組み込みのクロックフォードスクリプトの代わりをするJSONエンコーディングとデコーディングを扱うjQueryを使って?

役に立ちましたか?

解決

あなたはこれをチェックすることをお勧めします: http://www.json.org/js.htmlする

他のヒント

あなたはクロスブラウザのJSONエンコーダ/デコーダを作るために「閉鎖ライブラリ」(グーグル)を使用することができます。

ただ、 http://closure-compiler.appspot.com/する

にアクセスしてください

とテキストフィールドに次のように挿入し、その後、「コンパイル」ヒットます:

// ==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;

jQueryのは、 jQuery.parseJSON() のでネイティブにJSON文字列をデコードすることができます。

エンコーディングのために、しかし、私は唯一のプラグインを知っている: jqueryの-JSONする

jQueryの内部で、この機能を必要としないため、これを行うには便利なメソッドを提供しません。

JSON.stringify()は、標準であり、そのオブジェクトのJSON文字列表現にオブジェクトを符号化する方法をお勧めします。これは、多くのブラウザでネイティブJSONオブジェクトのメソッドであり、あなたが、フォールバックを提供するために、(https://github.com/douglascrockford/JSON-js)json2.jsを使用することをお勧めします。

steweの答えに構築するには、のアドバンストのをオンにして閉鎖コンパイラはあなたに1つの文字変数の束を持つグローバルな名前空間を汚染するスクリプトを提供します。だから、私はちょうどそうのような無名関数の呼び出しでそれをラップします:

(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,a.a?a.a.call(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,a.a?a.a.call(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); })();

今、あなたは呼び出すことができます:

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

のjQueryを使用している場合が多いJSON.stringify()関数が必要とされていません。 jqueryのは、内蔵されたこれを処理する機能、例えば、サーバへのjavascriptのデータを送信するために、AJAXを使用した一般的なケースを取る:<のhref =「http://api.jquery.com/category/ajax/」から(例rel = "nofollowを"> 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);
});

送信するJavaScriptデータ上記すべての実施例において、自動的にjQueryによってシリアライズされます。

これらの場合のシリアライゼーションは、(JSON.Stringifyと同じではない)、代わりに、データは、HTMLクエリ文字列にシリアライズされる(参照: http://en.wikipedia.org/wiki/Query_string#Structure を)。

しかしシリアライズのこの形式は、(すべてではない)、ほとんどの罰金であるアプリケーション

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top