function buildParams(prefix, obj, traditional, add) { var name;
if (jQuery.isArray(obj)) { // Serialize array item. jQuery.each(obj, function (i, v) { if (traditional || rbracket.test(prefix)) { // Treat each array item as a scalar. add(prefix, v);
} else { // Item is non-scalar (array or object), encode its numeric index. buildParams(prefix + "[" + (typeof v === "object" ? i : "") + "]", v, traditional, add); } });
} else if (!traditional && jQuery.type(obj) === "object") { // Serialize object item. for (name in obj) { buildParams(prefix + "[" + name + "]", obj[name], traditional, add); }
// Serialize an array of form elements or a set of // key/values into a query string jQuery.param = function (a, traditional) { var prefix, s = [], add = function (key, value) { // If value is a function, invoke it and return its value value = jQuery.isFunction(value) ? value() : (value == null ? "" : value); s[s.length] = encodeURIComponent(key) + "=" + encodeURIComponent(value); };
// Set traditional to true for jQuery if (traditional === undefined) { traditional = jQuery.ajaxSettings && jQuery.ajaxSettings.traditional; }
// If an array was passed in, assume that it is an array of form elements. if (jQuery.isArray(a) || (a.jquery && !jQuery.isPlainObject(a))) { // Serialize the form elements jQuery.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, "+"); };
jQuery.fn.extend({ serialize: function () { return jQuery.param(this.serializeArray()); }, serializeArray: function () { return this.map(function () { // Can add propHook for "elements" to filter or add form elements var elements = jQuery.prop(this, "elements"); return elements ? jQuery.makeArray(elements) : this; }) .filter(function () { var type = this.type; // Use .is(":disabled") so that fieldset[disabled] works return this.name && !jQuery(this).is(":disabled") && rsubmittable.test(this.nodeName) && !rsubmitterTypes.test(type) && (this.checked || !rcheckableType.test(type)); }) .map(function (i, elem) { var val = jQuery(this).val();
publicServicesUser.setUpdateUser(sessionData.getUserId()); publicServicesUser.setUpdateUserName(sessionData.getUserName()); if (publicServicesUser.getUpdateTime() == null) { publicServicesUser.setUpdateTime(new Date()); }
Map
JSON.parse()
将json字符串转换成json对象
Json2插件源码
var JSON; JSON || (JSON = {}), function () { function f(a) { return a }
function quote(a) { return escapable.lastIndex = 0, escapable.test(a) ? '"' + a.replace(escapable, function (a) { var b = meta[a]; return typeof b == "string" ? b : "\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4) }) + '"' : '"' + a + '"' }
function str(a, b) { var c, d, e, f, g = gap, h, i = b[a]; i && typeof i == "object" && typeof i.toJSON == "function" && (i = i.toJSON(a)), typeof rep == "function" && (i = rep.call(b, a, i)); switch (typeof i) { case "string": return quote(i); case "number": return isFinite(i) ? String(i) : "null"; case "boolean": case "null": return String(i); case "object": if (!i) return "null"; gap += indent, h = []; if (Object.prototype.toString.apply(i) === "[object Array]") { f = i.length; for (c = 0; c return e = h.length === 0 ? "[]" : gap ? "[n" + gap + h.join(",n" + gap) + "n" + g + "]" : "[" + h.join(",") + "]", gap = g, e } if (rep && typeof rep == "object") { f = rep.length; for (c = 0; c } else for (d in i) Object.prototype.hasOwnProperty.call(i, d) && (e = str(d, i), e && h.push(quote(d) + (gap ? ": " : ":") + e)); return e = h.length === 0 ? "{}" : gap ? "{n" + gap + h.join(",n" + gap) + "n" + g + "}" : "{" + h.join(",") + "}", gap = g, e } } "use strict", typeof Date.prototype.toJSON != "function" && (Date.prototype.toJSON = function (a) { return isFinite(this.valueOf()) ? this.getUTCFullYear() + "-" + f(this.getUTCMonth() + 1) + "-" + f(this.getUTCDate()) + "T" + f(this.getUTCHours()) + ":" + f(this.getUTCMinutes()) + ":" + f(this.getUTCSeconds()) + "Z" : null }, String.prototype.toJSON = Number.prototype.toJSON = Boolean.prototype.toJSON = function (a) { return this.valueOf() }); var cx = /[u0000u00adu0600-u0604u070fu17b4u17b5u200c-u200fu2028-u202fu2060-u206fufeffufff0-uffff]/g, escapable = /[\"x00-x1fx7f-x9fu00adu0600-u0604u070fu17b4u17b5u200c-u200fu2028-u202fu2060-u206fufeffufff0-uffff]/g, gap, indent, meta = { "b": "\b", "t": "\t", "n": "\n", "f": "\f", "r": "\r", '"': '\"', "\": "\\" }, rep; typeof JSON.stringify != "function" && (JSON.stringify = function (a, b, c) { var d; gap = "", indent = ""; if (typeof c == "number") for (d = 0; d else typeof c == "string" && (indent = c); rep = b; if (!b || typeof b == "function" || typeof b == "object" && typeof b.length == "number") return str("", { "": a }); throw new Error("JSON.stringify") }), typeof JSON.parse != "function" && (JSON.parse = function (text, reviver) { function walk(a, b) { var c, d, e = a[b]; if (e && typeof e == "object") for (c in e) Object.prototype.hasOwnProperty.call(e, c) && (d = walk(e, c), d !== undefined ? e[c] = d : delete e[c]); return reviver.call(a, b, e) } var j; text = String(text), cx.lastIndex = 0, cx.test(text) && (text = text.replace(cx, function (a) { return "\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4) })); if (/^[],:{}s]*$/.test(text.replace(/\(?:["\/bfnrt]|u[0-9a-fA-F]{4})/g, "@").replace(/"[^"\nr]*"|true|false|null|-?d+(?:.d*)?(?:[eE][+-]?d+)?/g, "]").replace(/(?:^|:|,)(?:s*[)+/g, ""))) return j = eval("(" + text + ")"), typeof reviver == "function" ? walk({ "": j }, "") : j; throw new SyntaxError("JSON.parse") }) }();
其他序列化插件
var params = $('#search_form').serializeArray(); $Core.util.QueryFormSerializeArray("search_form") var v = $Core.util.QueryFormSerializeString('search_form'); paras.querystr = v;
源码
(function (window, $, undefined) { var _util = function () { /* * 序列化Form 返回数组对象 * @param {any} domid domid */ this.QueryFormSerializeArray = function (domid) { var form = null; if (typeof (domid) === "string") from = $('#' + domid); else from = domid; var p = from.serializeArray(); var s = $.map(p, function (n) { var t = $("#" + n.name); if (t.size() == 0) t = from.find("*[textboxname='" + n.name + "']"); var query = {}; var s = $.trim(t.attr('data-query')); if (s) { if (s.substring(0, 1) != '{') { s = '{' + s + '}'; } query = (new Function('return ' + s))(); if (query.filed) { query.name = query.filed; //query.remove("filed"); } } //console.log(query); return $.extend({}, n, query); });
return s; }; /** * 序列化Form 返回字符串 * @param {string} domid * @return {any} 序列化后的 */ this.QueryFormSerializeString = function (domid) { var s = this.QueryFormSerializeArray(domid); var strpars = JSON.stringify(s); return strpars; }; }; $Core.util = new _util(); })(window, jQuery);