سؤال

يبدو أن JQuery لديه القدرة على فك تشفير كائن أو سلسلة معينة في كائن JSON. ومع ذلك ، لدي كائن JS الذي أحتاجه إلى إرساله مرة أخرى إلى الخادم ولا أجد أي أداة في jQuery التي تلتف وظيفة json.stringify (). تم العثور على هذه الوظيفة في Chrome و Safari 4 و FF3.6 و IE8 ولكن لم يتم العثور عليها في المتصفحات السابقة. يمكنني استخدامه أصليًا في المتصفحات التي تدعمها ، لكنني مضطر إلى العودة إلى استخدام البرامج النصية JSON من Crockford.

هل هناك بعض المدمج مع jQuery الذي يتولى ترميز JSON وفك تشفيره يحل محل البرامج النصية Crockford؟

هل كانت مفيدة؟

المحلول

قد ترغب في التحقق من ذلك:http://www.json.org/js.html

نصائح أخرى

يمكنك استخدام "مكتبة الإغلاق" (Google) لإنشاء متصفح متقاطع 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.parseJSON().

للترميز ، لا أعرف إلا عن البرنامج المساعد: jquery-json

لا تحتاج jQuery إلى هذه الوظيفة داخليًا وبالتالي لا توفر طريقة للراحة للقيام بذلك.

json.stringify () هي الطريقة المعيارية والمصدرة لترميز كائن على تمثيل سلسلة JSON لهذا الكائن. إنها طريقة لكائن JSON الأصلي في العديد من المتصفحات ، ويوصى بأنك تستخدم JSON2.JS (https://github.com/douglascrockford/json-js) لتوفير احتياطي.

للبناء على إجابة Stewe ، إغلاق مترجم مع متقدم

(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'});

غالبًا ما تكون وظيفة json.stringify () غير مطلوبة عند استخدام jQuery. خذ على سبيل المثال الحالة الشائعة لاستخدام AJAX لإرسال بيانات JavaScript إلى الخادم ، لدى JQuery وظائف مدمجة للتعامل مع هذا: (أمثلة من 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