Function object2string (OBJ) <br/>{< br/> var val, output = ""; <br/> If (OBJ) <br/>{< br/> output + = "{"; <br/> for (var I in OBJ) <br/>{< br/> val = OBJ [I]; <br/> switch (typeof Val) <br/>{< br/> case ("object "): If (Val [0]) {output + = I + ":" + array2string (VAL) + "," ;}else {output + = I + ": "+ object2string (VAL) +", ";}break; <br/> case (" string "): Output + = I +": '"+ encodeuri (VAL) + "',"; break; <br/> default: Output + = I + ":" + Val + ","; <br/>}< br/> output = output. substring (0, output. length-1) + "}"; <br/>}< br/> return Unescape (output); <br/>}< br/> function array2string (array) <br/> {<br/> var output = ""; <br/> If (array) <br/> {<br/> output + = "["; <br/> for (var I in array) <br/>{< br/> val = array [I]; <br/> switch (typeof Val) <br/>{< br/> case ("object"): <br/> If (Val [0]) <br/> {output + = array2string (VAL) + "," ;}< br/> else <br/> {output + = object2string (VAL) + "," ;}< br/> break; <br/> case ("string"): <br/> output + = "'" + encodeuri (VAL) + "',"; <br/> break; <br/> default: <br/> output + = Val + ","; <br/>}< br/> output = output. substring (0, output. length-1) + "]"; <br/>}< br/> return Unescape (output); <br/>}< br/> function string2object (string) <br/>{< br/> eval ("Var result =" + decodeuri (string); return result; <br/>}< br/> function string2array (string) <br/>{< br/> eval ("Var result =" + decodeuri (string); return result; <br/>}
Test code:
Function coworker (name, age, Dept) {This. name = Name; this. age = age; this. department = dept ;}< br/> var OBJ ={}; <br/> obj. name = "DETP one"; <br/> obj. action = "sale"; <br/> var Kevin = new coworker ("Kevin", 28, OBJ); <br/> var objstr = object2string (Kevin ); <br/> alert (objstr); <br/>
Used materials:
VaR STR = "http://hi.baidu.com/bbjjss2008l? Country = China & name = JZ "; <br/> var urlstr = escape (STR); <br/> document. write ("escape method encryption: <br/>" + urlstr + "<br/> after decryption, the URL string is: <br/> "+ Unescape (STR); <br/> urlstr = encodeuri (STR); <br/> document. write ("<br/> encodeuri: Method encryption: <br/>" + urlstr + "<br/> the decrypted URL string is: <br/> "+ decodeuri (STR); <br/> urlstr = encodeuricomponent (STR); <br/> document. write ("<br/> encodeuricomponent: Method encryption: <br/> "+ urlstr +" <br/> the decrypted URL string is: <br/> "+ decodeuricomponent (STR); <br/>