代码如下:
$.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;
};
代码如下:
$.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('');
};