我自己找到了解决方案,我模仿了序列化功能并创建了一个简单的插件。如有不足请指出,谢谢
(function($) {
$.fn.paramJson = function(a, traditional) {
var prefix, s = [], add = function(key, value) {
// If value is a function, invoke it and return its value
value = $.isFunction(value) ? value() : (value == null ? ""
: value);
s[s.length] = "\"" + encodeURIComponent(key) + "\"" + ":" + "\""
+ encodeURIComponent(value) + "\"";
};
// Set traditional to true for jQuery <= 1.3.2 behavior.
if (traditional === undefined) {
traditional = $.ajaxSettings
&& $.ajaxSettings.traditional;
}
// If an array was passed in, assume that it is an array of form
// elements.
if ($.isArray(a) || (a.jquery && !$.isPlainObject(a))) {
// Serialize the form elements
$.each(a, function() {
add(this.name, this.value);
});
} else {
// If traditional, encode the "old" way (the way 1.3.2 or older
// did it), otherwise encode params recursively.
for (prefix in a) {
buildParams(prefix, a[prefix], traditional, add);
}
}
// Return the resulting serialization
return "{" + s.join(",").replace(r20, "+") + "}";
};
$.fn.serializeJson = function() {
return $.parseJSON($.paramJson( this.serializeArray()));
};
})(jQuery)