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();
}
以下是来自http://blog.csdn.net/do_it__/article/details/6740303
我就照抄过来了。。
/* 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();
}
