JSON.stringify()
方法可以将任意的 JavaScript 值序列化成 JSON 字符串。
如果在IE8下使用,需要标准的doctype并且添加X-UA-Compatible
meta。
<!DOCTYPE html> ... <meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
如果使用IE6、7,那么需要引入json2.js。
json2.js: This file creates a JSON property in the global object, if there
isn‘t already one, setting its value to an object containing a stringify
method and a parse method. The parse method uses the eval method to do the
parsing, guarding it with several regular expressions to defend against
accidental code execution hazards. On current browsers, this file does nothing,
prefering the built-in JSON object.
语法
JSON.stringify(value[, replacer [, space]])
value
replacer
可选space
可选关于序列化,有下面三点注意事项:
undefined、
任意的函数值以及 symbol 值,在序列化过程中会被忽略(出现在非数组对象的属性值中时)或者被转换成 null
(出现在数组中时)。replacer
参数中强制指定包含了它们。JSON.stringify({}); // ‘{}‘ JSON.stringify(true); // ‘true‘ JSON.stringify("foo"); // ‘"foo"‘ JSON.stringify([1, "false", false]); // ‘[1,"false",false]‘ JSON.stringify({ x: 5 }); // ‘{"x":5}‘ JSON.stringify({x: 5, y: 6}); // ‘{"x":5,"y":6}‘ 或者 ‘{"y":6,"x":5}‘ 都可能 JSON.stringify([new Number(1), new String("false"), new Boolean(false)]); // ‘[1,"false",false]‘ JSON.stringify({x: undefined, y: Object, z: Symbol("")}); // ‘{}‘ JSON.stringify([undefined, Object, Symbol("")]); // ‘[null,null,null]‘ JSON.stringify({[Symbol("foo")]: "foo"}); // ‘{}‘ JSON.stringify({[Symbol.for("foo")]: "foo"}, [Symbol.for("foo")]); // ‘{}‘ JSON.stringify({[Symbol.for("foo")]: "foo"}, function (k, v) { if (typeof k === "symbol"){ return "a symbol"; } }); // ‘{}‘
space
参数space 参数用来控制结果字符串里面的间距。如果是一个数字, 则在字符串化时每一级别会比上一级别缩进多这个数字值的空格(最多10个空格);如果是一个字符串,则每一级别会比上一级别多缩进用该字符串(或该字符串的前十个字符)。
JSON.stringify({ a: 2 }, null, " "); // ‘{\n "a": 2\n}‘
使用制表符(\t)来缩进:
JSON.stringify({ uno: 1, dos : 2 }, null, ‘\t‘) // ‘{ \ // "uno": 1, \ // "dos": 2 \ // }‘
如果一个被序列化的对象拥有 toJSON
方法,那么该 toJSON
方法就会覆盖该对象默认的序列化行为:不是那个对象被序列化,而是调用 toJSON
方法后的返回值会被序列化,例如:
var obj = { foo: ‘foo‘, toJSON: function () { return ‘bar‘; } }; JSON.stringify(obj); // ‘"bar"‘ JSON.stringify({x: obj}); // ‘{"x":"bar"}‘
一些时候,你想存储用户创建的一个对象,并且,即使在浏览器被关闭后仍能恢复该对象。下面的例子是 JSON.stringify
适用于这种情形的一个样板:
// 创建一个示例数据 var session = { ‘screens‘ : [], ‘state‘ : true }; session.screens.push({"name":"screenA", "width":450, "height":250}); session.screens.push({"name":"screenB", "width":650, "height":350}); session.screens.push({"name":"screenC", "width":750, "height":120}); session.screens.push({"name":"screenD", "width":250, "height":60}); session.screens.push({"name":"screenE", "width":390, "height":120}); session.screens.push({"name":"screenF", "width":1240, "height":650}); // 使用 JSON.stringify 转换为 JSON 字符串 // 然后使用 localStorage 保存在 session 名称里 localStorage.setItem(‘session‘, JSON.stringify(session)); // 然后是如何转换通过 JSON.stringify 生成的字符串,该字符串以 JSON 格式保存在 localStorage 里 var restoredSession = JSON.parse(localStorage.getItem(‘session‘)); // 现在 restoredSession 包含了保存在 localStorage 里的对象 console.log(restoredSession);
原文:http://www.cnblogs.com/Leman/p/4357678.html