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