日期:2014-05-16  浏览次数:20388 次

js object 转化为string对象

?

/* object to string */
	function obj2str(o){
		var r = [], i, j = 0, len;
		if(o == null) {
			return o;
		}
		if(typeof o == 'string'){
			return '"'+o+'"';
		}
		if(typeof o == 'object'){
			if(!o.sort){
				r[j++]='{';
				for(i in o){
					r[j++]= '"';
					r[j++]= i;
					r[j++]= '":';
					r[j++]= obj2str(o[i]);
					r[j++]= ',';
				}
				//可能的空对象
				//r[r[j-1] == '{' ? j:j-1]='}';
				r[j-1] = '}';
			}else{
				r[j++]='[';
				for(i =0, len = o.length;i < len; ++i){
					r[j++] = obj2str(o[i]);
					r[j++] = ',';
				}
				//可能的空数组
				r[len==0 ? j:j-1]=']';
			}
			return r.join('');
		}
		return o.toString();
	}

?

?

推荐:android开发网

原文:http://blog.csdn.net/do_it__/article/details/6740303

?