推广 热搜: 劳务  2024  海南  合同  云南  江苏  劳务系统  2023  劳务派遣管理软件  劳务派遣管理系统 

ie下解决json序列化问题

   2024-06-12 60
核心提示:p//CreateaJSONobjectonlyifonedoesnotalreadyexist.Wecreatethe//methodsinaclosuretoavoidcreatingglobalvariables.if(typeofJ
<p>//CreateaJSONobjectonlyifonedoesnotalreadyexist.Wecreatethe//methodsinaclosuretoavoidcreatingglobalvariables.if(typeofJSON!=='object'){JSON={};}(function(){'usestrict';functionf(n){//Formatintegerstohaveatleasttwodigits.returnn<10?'0'+n:n;}if(typeofDate.prototype.toJSON!=='function'){Date.prototype.toJSON=function(){returnisFinite(this.valueOf())?this.getUTCFullYear()+'-'+f(this.getUTCMonth()+1)+'-'+f(this.getUTCDate())+'T'+f(this.getUTCHours())+':'+f(this.getUTCMinutes())+':'+f(this.getUTCSeconds())+'Z':null;};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){returnthis.valueOf();};}varcx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={//tableofcharactersubstitutions'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},rep;functionquote(string){//Ifthestringcontainsnocontrolcharacters,noquotecharacters,andno//backslashcharacters,thenwecansafelyslapsomequotesaroundit.//Otherwisewemustalsoreplacetheoffendingcharacterswithsafeescape//sequences.escapable.lastIndex=0;returnescapable.test(string)?'"'+string.replace(escapable,function(a){varc=meta[a];returntypeofc==='string'?c:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4);})+'"':'"'+string+'"';}functionstr(key,holder){//Produceastringfromholder[key].vari,//Theloopcounter.k,//Thememberkey.v,//Themembervalue.length,mind=gap,partial,value=holder[key];//IfthevaluehasatoJSONmethod,callittoobtainareplacementvalue.if(value&&typeofvalue==='object'&&typeofvalue.toJSON==='function'){value=value.toJSON(key);}//Ifwewerecalledwithareplacerfunction,thencallthereplacerto//obtainareplacementvalue.if(typeofrep==='function'){value=rep.call(holder,key,value);}//Whathappensnextdependsonthevalue'stype.switch(typeofvalue){case'string':returnquote(value);case'number'://JSONnumbersmustbefinite.Encodenon-finitenumbersasnull.returnisFinite(value)?String(value):'null';case'boolean':case'null'://Ifthevalueisabooleanornull,convertittoastring.Note://typeofnulldoesnotproduce'null'.Thecaseisincludedherein//theremotechancethatthisgetsfixedsomeday.returnString(value);//Ifthetypeis'object',wemightbedealingwithanobjectoranarrayor//null.case'object'://DuetoaspecificationblunderinECMAscript,typeofnullis'object',//sowatchoutforthatcase.if(!value){return'null';}//Makeanarraytoholdthepartialresultsofstringifyingthisobjectvalue.gap+=indent;partial=[];//Isthevalueanarray?if(Object.prototype.toString.apply(value)==='[objectArray]'){//Thevalueisanarray.Stringifyeveryelement.Usenullasaplaceholder//fornon-JSONvalues.length=value.length;for(i=0;i<length;i+=1){partial[i]=str(i,value)||'null';}//Joinalloftheelementstogether,separatedwithcommas,andwrapthemin//brackets.v=partial.length===0?'[]':gap?'[\n'+gap+partial.join(',\n'+gap)+'\n'+mind+']':'['+partial.join(',')+']';gap=mind;returnv;}//Ifthereplacerisanarray,useittoselectthememberstobestringified.if(rep&&typeofrep==='object'){length=rep.length;for(i=0;i<length;i+=1){if(typeofrep[i]==='string'){k=rep[i];v=str(k,value);if(v){partial.push(quote(k)+(gap?':':':')+v);}}}}else{//Otherwise,iteratethroughallofthekeysintheobject.for(kinvalue){if(Object.prototype.hasOwnProperty.call(value,k)){v=str(k,value);if(v){partial.push(quote(k)+(gap?':':':')+v);}}}}//Joinallofthemembertextstogether,separatedwithcommas,//andwraptheminbraces.v=partial.length===0?'{}':gap?'{\n'+gap+partial.join(',\n'+gap)+'\n'+mind+'}':'{'+partial.join(',')+'}';gap=mind;returnv;}}//IftheJSONobjectdoesnotyethaveastringifymethod,giveitone.if(typeofJSON.stringify!=='function'){JSON.stringify=function(value,replacer,space){//Thestringifymethodtakesavalueandanoptionalreplacer,andanoptional//spaceparameter,andreturnsaJSONtext.Thereplacercanbeafunction//thatcanreplacevalues,oranarrayofstringsthatwillselectthekeys.//Adefaultreplacermethodcanbeprovided.Useofthespaceparametercan//producetextthatismoreeasilyreadable.vari;gap='';indent='';//Ifthespaceparameterisanumber,makeanindentstringcontainingthat//manyspaces.if(typeofspace==='number'){for(i=0;i<space;i+=1){indent+='';}//Ifthespaceparameterisastring,itwillbeusedastheindentstring.}elseif(typeofspace==='string'){indent=space;}//Ifthereisareplacer,itmustbeafunctionoranarray.//Otherwise,throwanerror.rep=replacer;if(replacer&&typeofreplacer!=='function'&&(typeofreplacer!=='object'||typeofreplacer.length!=='number')){thrownewError('JSON.stringify');}//Makeafakerootobjectcontainingourvalueunderthekeyof''.//Returntheresultofstringifyingthevalue.returnstr('',{'':value});};}//IftheJSONobjectdoesnotyethaveaparsemethod,giveitone.if(typeofJSON.parse!=='function'){JSON.parse=function(text,reviver){//Theparsemethodtakesatextandanoptionalreviverfunction,andreturns//aJavascriptvalueifthetextisavalidJSONtext.varj;functionwalk(holder,key){//Thewalkmethodisusedtorecursivelywalktheresultingstructureso//thatmodificationscanbemade.vark,v,value=holder[key];if(value&&typeofvalue==='object'){for(kinvalue){if(Object.prototype.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v;}else{deletevalue[k];}}}}returnreviver.call(holder,key,value);}//Parsinghappensinfourstages.Inthefirststage,wereplacecertain//Unicodecharacterswithescapesequences.Javascripthandlesmanycharacters//incorrectly,eithersilentlydeletingthem,ortreatingthemaslineendings.text=String(text);cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4);});}//Inthesecondstage,werunthetextagainstregularexpressionsthatlook//fornon-JSONpatterns.Weareespeciallyconcernedwith'()'and'new'//becausetheycancauseinvocation,and'='becauseitcancausemutation.//Butjusttobesafe,wewanttorejectallunexpectedforms.//Wesplitthesecondstageinto4regexpoperationsinordertoworkaround//cripplinginefficienciesinIE'sandSafari'sregexpengines.Firstwe//replacetheJSONbackslashpairswith'@'(anon-JSONcharacter).Second,we//replaceallsimplevaluetokenswith']'characters.Third,wedeleteall//openbracketsthatfollowacolonorcommaorthatbeginthetext.Finally,//welooktoseethattheremainingcharactersareonlywhitespaceor']'or//','or':'or'{'or'}'.Ifthatisso,thenthetextissafeforeval.if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){//Inthethirdstageweusetheevalfunctiontocompilethetextintoa//Javascriptstructure.The'{'operatorissubjecttoasyntacticambiguity//inJavascript:itcanbeginablockoranobjectliteral.Wewrapthetext//inparenstoeliminatetheambiguity.j=eval('('+text+')');//Intheoptionalfourthstage,werecursivelywalkthenewstructure,passing//eachname/valuepairtoareviverfunctionforpossibletransformation.returntypeofreviver==='function'?walk({'':j},''):j;}//IfthetextisnotJSONparseable,thenaSyntaxErroristhrown.thrownewSyntaxError('JSON.parse');};}}());</p>
 
标签: 劳务派遣系统
反对 0举报 0 收藏 0 打赏 0评论 0
 
更多>同类资讯
推荐图文
推荐资讯
点击排行
网站首页  |  关于我们  |  联系方式  |  使用协议  |  版权隐私  |  网站地图  |  排名推广  |  广告服务  |  积分换礼  |  网站留言  |  RSS订阅  |  违规举报  |  粤ICP备18079127号  |  粤ICP备18079127号