Object序列化成一个字符串(JSON的序列化)

来源:互联网 发布:数据分析预测软件 编辑:程序博客网 时间:2024/05/16 07:09
//1function objToStr(obj){ var type = typeof obj; if(type === 'number') return obj; if(type === 'string') return '"' + encodeStr(obj) + '"'; if(obj == null || type !== 'object') return '""'; var list = []; if(obj instanceof Array) { var len = obj.length; while(len--){ list.unshift(objToStr(obj[len])); } return '[' + list.join(',') + ']'; } for(var k in obj){ list.push('"' + encodeStr(k) + '":' + objToStr(obj[k])); } return '{' + list.join(',') + '}'; } function encodeStr(str){ return str.replace(/"/g, '\\\"'); }//2(function() {    "use strict";    var escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,        meta = {            '\b' : '\\b',            '\t' : '\\t',            '\n' : '\\n',            '\f' : '\\f',            '\r' : '\\r',            '"' : '\\"',            '\\' : '\\\\'        };    function quote(string) {        return '"' + string.replace(escapable, function(a) {                var c = meta[a];                return typeof c === "string" ? c : "\\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4);            }) + '"';    }    function f(n) {        return n < 10 ? "0" + n : n;    }    function str(key, holder) {        var i, v, len, partial, value = holder[key], type = typeof value;        if (value && typeof value === "object" && typeof value.toJSON === "function") {            value = value.toJSON(key);            type = typeof value;        }        switch (type) {            case "string":                return quote(value);            case "number":                return isFinite(value) ? String(value) : "null";            case "boolean":                return String(value);            case "object":                if (!value) {                    return "null";                }                switch (Object.prototype.toString.call(value)) {                    case "[object Date]":                        return isFinite(value.valueOf()) ?                        '"' + value.getUTCFullYear() + "-" + f(value.getUTCMonth() + 1) + "-" + f(value.getUTCDate()) +                        "T" + f(value.getUTCHours()) + ":" + f(value.getUTCMinutes()) + ":" + f(value.getUTCSeconds()) + "Z" + '"' :                            "null";                    case "[object Array]":                        len = value.length;                        partial = [];                        for (i = 0; i < len; i++) {                            partial.push(str(i, value) || "null");                        }                        return "[" + partial.join(",") + "]";                    default:                        partial = [];                        for (i in value) {                            if (Object.prototype.hasOwnProperty.call(value, i)) {                                v = str(i, value);                                if (v) {                                    partial.push(quote(i) + ":" + v);                                }                            }                        }                        return "{" + partial.join(",") + "}";                }        }    }    function stringifyJSON(value) {        if (window.JSON && window.JSON.stringify) {            return window.JSON.stringify(value);        }        return str("", {"": value});    }    // 暴露stringifyjson的全局对象    window.stringifyJSON = stringifyJSON;}());
0 0
原创粉丝点击