js json转字符串

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<o.length;i++){
             r[r.length]=obj2str(o[i]);
             r[r.length]=",";
           }
           r[r.length-1]="]"
         }
         return r.join("");
       }
       return o.toString();
    }

 

posted on 2015-05-19 17:45  hzm_frank  阅读(144)  评论(0编辑  收藏  举报

导航