日期:2014-05-16  浏览次数:20415 次

json对象到string转换
   var JsonUti = {
            //定义换行符
            n: "\n",
            //定义制表符
            t: "\t",
            //转换String
            convertToString: function(obj) {
                return JsonUti.__writeObj(obj, 1);
            },
            //写对象
            __writeObj: function(obj    //对象
                    , level             //层次(基数为1)
                    , isInArray) {       //此对象是否在一个集合内
                //如果为空,直接输出null
                if (obj == null) {
                    return "null";
                }
                //为普通类型,直接输出值
                if (obj.constructor == Number || obj.constructor == Date || obj.constructor == String || obj.constructor == Boolean) {
                    var v = obj.toString();
                    var tab = isInArray ? JsonUti.__repeatStr(JsonUti.t, level - 1) : "";
                    if (obj.constructor == String || obj.constructor == Date) {
                        //时间格式化只是单纯输出字符串,而不是Date对象
                        return tab + ("\"" + v + "\"");
                    }
                    else if (obj.constructor == Boolean) {
                        return tab + v.toLowerCase();
                    }
                    else {
                        return tab + (v);
                    }