javascript将对象转换为字符串

来源:互联网 发布:小米手机淘宝商城 编辑:程序博客网 时间:2024/05/21 02:34
function obj2str(o){
   var r = [];
   if(typeof o == "string" || o== null) {
    return o;
   }
   if(typeof o =="object"){
    if(!o.sort){
      r[0]="{"
      for(var i in o){
        r[r.length]=i;
        r[r.length]=":";
        r[r.length]=obj2str(o[i]);
        r[r.length]=",";
      }
      r[r.length-1]="}"
    }else{
      r[0]="["
      for(var i =0;i
      // alert(o[i]);
      
        r[r.length]=obj2str(o[i]);
        r[r.length]=",";
      }
      r[r.length-1]="]"
    }
    return r.join("");
   }
   return o.toString();
}