javascript中將Object轉(zhuǎn)換為String函數(shù)代碼 (json str)
更新時(shí)間:2012年04月29日 23:48:12 作者:
下面的代碼就是想將Object轉(zhuǎn)換為String函數(shù),需要的朋友可以參考下
復(fù)制代碼 代碼如下:
function obj2str(o){
var r = [];
if(typeof o =="string") return "\""+o.replace(/([\'\"\\])/g,"\\$1").replace(/(\n)/g,"\\n").replace(/(\r)/g,"\\r").replace(/(\t)/g,"\\t")+"\"";
if(typeof o == "object"){
if(!o.sort){
for(var i in o)
r.push(i+":"+obj2str(o[i]));
if(!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)){
r.push("toString:"+o.toString.toString());
}
r="{"+r.join()+"}"
}else{
for(var i =0;i<o.length;i++)
r.push(obj2str(o[i]))
r="["+r.join()+"]"
}
return r;
}
return o.toString();
}
相關(guān)文章
JS實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊箭頭旋轉(zhuǎn)180度功能
這篇文章主要介紹了JS實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊箭頭旋轉(zhuǎn)180度的效果,通過點(diǎn)擊三角按鈕旋轉(zhuǎn)180度,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2024-02-02
js插件設(shè)置innerHTML時(shí)在IE8下提示“未知運(yùn)行時(shí)錯(cuò)誤”解決方法
這篇文章主要介紹了js插件設(shè)置innerHTML時(shí)在IE8下提示“未知運(yùn)行時(shí)錯(cuò)誤”解決方法,較為詳細(xì)的分析了錯(cuò)誤的原因及對應(yīng)的解決方法,需要的朋友可以參考下2015-04-04

