function toParameter(obj){
if(obj instanceof Number || obj instanceof String || obj instanceof Boolean){
return obj.valueOf();
}
var arr = [], arrStr, arrlen, i;
if(obj instanceof Array){
arrlen = obj.length;
for(i = 0; i < arrlen; i++){
if(typeof obj[i] == 'string' || typeof obj[i] == 'number' || typeof obj[i] == 'boolean'){
arr.push(obj[i]);
}else if(obj[i] instanceof Array){
arr.push(encodeURIComponent(arguments.callee(obj[i])));
}else if(obj[i] instanceof Object){ //Array's instance is also a instance of Object
arr.push(encodeURIComponent(arguments.callee(obj[i])));
}else{
}
}
return arr.join('@');
}else if(obj instanceof Object){
var name, value;
for(name in obj){
switch(typeof obj[name]){
case 'string':
case 'number':
case 'boolean':
value = obj[name];
break;
case 'object':
value = arguments.callee(obj[name]);
break;
case 'undefined':
case 'function':
value = '';
break;
}
arr.push(name + '=' + encodeURIComponent(value));
}
return arr.join('&');
}
}
function parseParameter(url){
if(url.indexOf('@') > -1){
var arr = [], i, arrlen, resultArr = [];
arr = url.split('@');
arrlen = arr.length;
for(i = 0; i < arrlen; i++){
var str = decodeURIComponent(arr[i]);
if(str.indexOf('=') < 0 && str.indexOf('@') < 0 && str.indexOf('&') < 0){
resultArr.push(str);
}else{
var element = arguments.callee(str);
var resultLen = resultArr.length;
resultArr[resultLen] = element; //push just for string
}
}
return resultArr;
}else if(url.indexOf('&') > -1){
var arr = [], i, arrlen, obj = {}, oneKeyValue;
arr = url.split('&');
arrlen = arr.length;
for(i = 0; i < arrlen; i++){
oneKeyValue = arr[i];
oneKeyValue = oneKeyValue.split('=');
var name = oneKeyValue[0];
var value = decodeURIComponent(oneKeyValue[1]);
if(value.indexOf('&') > -1 || value.indexOf('@') > -1 || value.indexOf('=') > -1){
value = arguments.callee(value);
}
obj[name] = value;
}
return obj;
}
}
//测试用例1
var json = [{
aaa : 'aaaa1',
bbb : 323
}, {
aaa : 'aaaa2',
bbb : [
'b22', 2, true
]
}];
//测试用例2
var obj = [[1, 'aa'],[2,'bb'],{c : 3, d : ['d1', 'd2']}];
var url = toParameter(obj);
var aa = parseParameter(url);
alert(aa);