javascript中將Object轉(zhuǎn)換為String函數(shù)代碼(jsonstr)_javascript技巧
來源:懂視網(wǎng)
責(zé)編:小采
時(shí)間:2020-11-27 21:03:00
javascript中將Object轉(zhuǎn)換為String函數(shù)代碼(jsonstr)_javascript技巧
javascript中將Object轉(zhuǎn)換為String函數(shù)代碼(jsonstr)_javascript技巧: 代碼如下: 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)+
導(dǎo)讀javascript中將Object轉(zhuǎn)換為String函數(shù)代碼(jsonstr)_javascript技巧: 代碼如下: 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)+

代碼如下:
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
r.push(obj2str(o[i]))
r="["+r.join()+"]"
}
return r;
}
return o.toString();
}
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
javascript中將Object轉(zhuǎn)換為String函數(shù)代碼(jsonstr)_javascript技巧
javascript中將Object轉(zhuǎn)換為String函數(shù)代碼(jsonstr)_javascript技巧: 代碼如下: 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)+