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

JavaScript中json对象序列化转换

jquery实现方法是:
window.JSON && window.JSON.parse ?window.JSON.parse( data ):(new Function("return " + data))();
ext,dojo实现方法是:
eval("(" + json + ")"); // Object

比较好的方式:
JSON = {
    useHasOwn: ({}.hasOwnProperty ? true: false),
    pad: function(n) {
        return n < 10 ? "0" + n: n;
    },
    m: {
        "\b": '\\b',
        "\t": '\\t',
        "\n": '\\n',
        "\f": '\\f',
        "\r": '\\r',
        '"': '\\"',
        "\\": '\\\\'
    },
    encodeString: function(s) {
        if (/["\\\x00-\x1f]/.test(s)) {
            return '"' + s.replace(/([\x00-\x1f\\"])/g,
            function(a, b) {
                var c = m[b];
                if (c) {
                    return c;
                }
                c = b.charCodeAt();
                return "\\u00" + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
            }) + '"';
        }
        return '"' + s + '"';
    },
    encodeArray: function(o) {
        var a = ["["],b, i, l = o.length,v;
        for (i = 0; i < l; i += 1) {
            v = o[i];
            switch (typeof v) {
            case "undefined":
            case "function":
            case "unknown":
                break;
            default:
                if (b) {
                    a.push(',');
                }
                a.push(v === null ? "null": this.encode(v));
                b = true;
            }
        }
        a.push("]");
        return a.join("");
    },
    encodeDate: function(o) {
        return '"' + o.getFullYear() + "-" + pad(o.getMonth() + 1) + "-" + pad(o.getDate()) + "T" + pad(o.getHours()) + ":" + pad(o.getMinutes()) + ":" + pad(o.getSeconds()) + '"';},
    encode: function(o) {
        if (typeof o == "undefined" || o === null) {
            return "null";
        } else if (o instanceof Array) {
            return this.encodeArray(o);
        } else if (o instanceof Date) {
            return this.encodeDate(o);
        } else if (typeof o == "string") {
            return this.encodeString(o);
        } else if (typeof o == "number") {
            return isFinite(o) ? String(o) : "null";
        } else if (typeof o == "boolean") {
            return String(o);
        } else {
            var self = this;
            var a = ["{"],b,i,v;
            for (i in o) {
                if (!this.useHasOwn || o.hasOwnProperty(i)) {
                    v = o[i];
                    switch (typeof v) {
                    case "undefined":
                    case "function":
                    case "unknown":
                        break;
                    default:
                        if (b) {
                            a.push(',');
                        }
                        a.push(self.encode(i), ":", v === null ? "null": self.encode(v));
                        b = true;
                    }
                }
            }
            a.push("}");
            return a.join("");
        }
    },
    decode: function(json) {
        return eval("(" + json + ')');
    }
};


JSON.decode('{a:1}')
JSON.encode({a:1,b:{c:'2'}})


参考:
http://www.ramirezcobos.com/2009/12/30/json-jquery-plugin/
http://stackoverflow.com/questions/1184624/serialize-form-to-json-with-jquery