语法
JSON.stringify(value[, replacer [, space]])
replacer为函数
function replacer(key, value) {
if (typeof value === "string") {
return undefined;
}
return value;
}
var foo = {foundation: "Mozilla", model: "box", week: 45, transport: "car", month: 7};
var jsonString = JSON.stringify(foo, replacer); // "{"week":45,"month":7}"
replacer为数组,只保留存在于数组中的key
var foo = {foundation: "Mozilla", model: "box", week: 45, transport: "car", month: 7};
var jsonString = JSON.stringify(foo, ['week', 'month']); // "{"week":45,"month":7}"
space
JSON.stringify({ uno: 1, dos : 2 }, null, '\t')
// '{ \
// "uno": 1, \
// "dos": 2 \
// }'
其他MDN中介绍的很详细
网友评论