时间:2020-12-16 09:05:26 | 栏目:JavaScript代码 | 点击:次
var parseParam=function(param, key){ varparamStr=""; if(paraminstanceof String||param instanceof Number||param instanceof Boolean){ paramStr+="&"+key+"="+encodeURIComponent(param); }else{ $.each(param,function(i){ vark=key==null?i:key+(paraminstanceof Array?"["+i+"]":"."+i); paramStr+='&'+parseParam(this, k); }); } returnparamStr.substr(1); };
//调用:
var obj={name:'tom','class':{className:'class1'},classMates:[{name:'lily'}]};
parseParam(obj);
结果:"name=tom&class.className=class1&classMates[0].name=lily"
parseParam(obj,'stu');
结果:"stu.name=tom&stu.class.className=class1&stu.classMates[0].name=lily"
将string 转换成json对象
var define = "{name: 'Michael ',email: '17bity@gmail.com '} ";
var data = eval( '( ' + define + ') ');
alert(data.name);