@@ -599,4 +599,77 @@ function digitUppercase (n) {
599599 */
600600function isArray ( a ) {
601601 Array . isArray ? Array . isArray ( a ) : Object . prototype . toString . call ( a ) === '[object Array]' ;
602- }
602+ }
603+
604+
605+ /**
606+ * JSON字符串转换为对象
607+ * @param string JSON字符串
608+ * @return {Object } 转换后对象
609+ */
610+ function jsonStringToObject ( string ) {
611+ try
612+ {
613+ return eval ( "(" + string + ")" ) ;
614+ }
615+ catch ( err )
616+ {
617+ return null ;
618+ }
619+ } ;
620+
621+ /**
622+ * 对象转JSON字符串
623+ * @param obj 对象
624+ * @return {string } JSON字符串
625+ */
626+ function objectToJsonString ( obj ) {
627+ var S = [ ] ;
628+ var J = null ;
629+
630+ var type = Object . prototype . toString . apply ( obj ) ;
631+
632+ if ( type === '[object Array]' )
633+ {
634+ for ( var i = 0 ; i < obj . length ; i ++ )
635+ {
636+ S . push ( objectToJsonString ( obj [ i ] ) ) ;
637+ }
638+ J = '[' + S . join ( ',' ) + ']' ;
639+ }
640+ else if ( type === '[object Date]' )
641+ {
642+ J = "new Date(" + obj . getTime ( ) + ")" ;
643+ }
644+ else if ( type === '[object RegExp]'
645+ || type === '[object Function]' )
646+ {
647+ J = obj . toString ( ) ;
648+ }
649+ else if ( type === '[object Object]' )
650+ {
651+ for ( var key in obj )
652+ {
653+ var value = objectToJsonString ( obj [ key ] ) ;
654+ if ( value != null )
655+ {
656+ S . push ( '"' + key + '":' + value ) ;
657+ }
658+ }
659+ J = '{' + S . join ( ',' ) + '}' ;
660+ }
661+ else if ( type === '[object String]' )
662+ {
663+ J = '"' + obj . replace ( / \\ / g, '\\\\' ) . replace ( / " / g, '\\"' ) . replace ( / \n / g, '' ) + '"' ;
664+ }
665+ else if ( type === '[object Number]' )
666+ {
667+ J = obj ;
668+ }
669+ else if ( type === '[object Boolean]' )
670+ {
671+ J = obj ;
672+ }
673+
674+ return J ;
675+ } ;
0 commit comments