JSON.stringfy

if (!window.JSON) {
        window.JSON = {
            stringify: function (vContent) {
                if (vContent instanceof Object) {
                    var sOutput = '';
                    if (vContent.constructor === Array) {
                        for (var nId = 0; nId < vContent.length; nId++) {
                            sOutput += this.stringify(vContent[nId]) + ',';
                        }
                        return '[' + sOutput.substr(0, sOutput.length - 1) + ']';
                    }
                    if (vContent.toString !== Object.prototype.toString) {
                        return '"' + vContent.toString().replace(/"/g, '\\$&') + '"';
                    }
                    for (var sProp in vContent) {
                        if (vContent[sProp]) {
                            sOutput += '"' + sProp.replace(/"/g, '\\$&') + '":' + this.stringify(vContent[sProp]) + ',';
                        }
                    }
                    return '{' + sOutput.substr(0, sOutput.length - 1) + '}';
                }
                return typeof vContent === 'string' ? '"' + vContent.replace(/"/g, '\\$&') + '"' : String(vContent);
            }
        };
    }


你可能感兴趣的:(js函数)