In jquery, $ ("# myform") is used "). serialize () can construct the content of the form into a querystring. For example, expressions such as width = 1680 & Height = 1050 can be converted to JSON
Expression {"width": "1680", "height": "1050 "}.
Sometimes, we need to further convert it into a JSON expression. Refer to the Ext. urldecode function in ext, we can implement a function used in jquery:
CopyCode The Code is as follows: $. par2json = function (string, overwrite ){
VaR OBJ = {},
Pairs = string. Split ('&'),
D = decodeuricomponent,
Name,
Value;
$. Each (pairs, function (I, pair ){
Pair = pair. Split ('= ');
Name = D (pair [0]);
Value = D (pair [1]);
OBJ [name] = overwrite |! OBJ [name]? Value:
[]. Concat (OBJ [name]). Concat (value );
});
Return OBJ;
};
If necessary, convert $. tojson (s) to JSON object.
If, in turn, the JSON expression is converted to the querystr parameter format, you can use the $. Param () method, or implement one by yourself, for example, the following code:Copy codeThe Code is as follows: $. json2par = function (O, pre ){
VaR UNDEF, Buf = [], key, E = encodeuricomponent;
For (key in O ){
UNDEF = O [Key] = 'undefined ';
$. Each (UNDEF? Key: O [Key], function (Val, I ){
Buf. Push ("&", E (key), "=", (Val! = Key |! UNDEF )? E (VAL ):"");
});
}
If (! PRE ){
Buf. Shift ();
Pre = "";
}
Return pre + Buf. Join ('');
};