今年青海湖开湖时间或将推迟

Baseline Widely available
百度 得益于投融两端的结构性变革,基建投资对中国经济高质量发展的支持作用将持续增强。

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

JSON.stringify() ???? JavaScript ??? ??? JSON ???? ?????. ?????, replacer? ??? ??? ?? ?? ? ?? ??? ? ??, ??? ??? ?? ??? ??? ??? ?????.

??? ??

console.log(JSON.stringify({ x: 5, y: 6 }));
// Expected output: '{"x":5,"y":6}'

console.log(
  JSON.stringify([new Number(3), new String("false"), new Boolean(false)]),
);
// Expected output: '[3,"false",false]'

console.log(JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] }));
// Expected output: '{"x":[10,null,null,null]}'

console.log(JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)));
// Expected output: '"2025-08-04T15:04:05.000Z"'

??

js
    JSON.stringify(value[, replacer[, space]])

????

value

JSON ???? ??? ?.

replacer Optional

???? ?? ??? ???? ??, ?? JSON ???? ??? ? ??? ???? ???? ?? ??????(whitelist)? ??? String ? Number ???? ??. ? ?? null ??? ???? ???, ??? ?? ???? JSON ??? ??? ????.

space Optional

???? ???? JSON ??? ??? ??? ????? ???? String ?? Number ??. ??? Number ??, ???? ???? ????(space)? ?? ????; ? ?? 10 ?? ?? 10 ?? ????. 1 ?? ?? ?? ????? ???? ?? ?? ????. ??? String ???, ? ???(?? ??? 10 ?? ???, ??? 10 ?? ??)? ???? ????. ? ?? ??? ???? ????(?? null ??), ??? ???? ???.

?? ?

??? ?? ???? JSON ???.

??

?? ??? ??? ?? TypeError(cyclic object value).

??

JSON.stringify()? ?? JSON ????? ????.

  • ??? ?? ??? ???? ?? ??? ??? ?? ???? ? ???? ???? ???. ?? ??? ????? ??? ??? ??? ???? ???.
  • Boolean, Number, String ???? ???? ? ? ???? ?? ??? ?? ??? ???(primitive) ??? ????.
  • undefined, ??, ??(symbol)? ??? ? ?????(?? ?? ?? ??) null ? ????(?? ?? ?? ??).
  • ??? ?? ??? ???? replacer ??? ?????? ??? ????.
  • ?? ???? ???? ????.
js
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(new Date(2006, 0, 2, 15, 4, 5));
// '"2025-08-04T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}' or '{"y":6,"x":5}'
JSON.stringify([new Number(1), new String("false"), new Boolean(false)]);
// '[1,"false",false]'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
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";
  }
});
// '{}'

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

replacer ?? ??

replacer ????? ?? ?? ??? ? ? ??. ??? ?? ???? ? key ? value, 2?? ????? ???. key ? ??? ??? ?????? this ????? ????. ? ???? ????? ? ??? ???? ?? ?? key? ?? ????, ? ???? ????? ? ??? ??? ? ??? ?? ????. ??? JSON ???? ????? ?? ?? ??????:

  • Number ? ????, JSON ???? ??? ? ? ?? ???? ???? ? ??? ??? ????.
  • String ? ????, ??? JSON ???? ??? ? ??? ??? ????.
  • Boolean ? ????, ??? JSON ???? ??? ? "true" ?? "false" ? ??? ??? ????.
  • ?? ??? ????, ? ??? replacer ??? ??? ??? ?? ???? ????? JSON ???? ??????. ? ??? ??? ???? JSON ???? ???? ???? ???.
  • undefined ? ????, ? ??? JSON ??? ??? ???? ???.

??: ??: replacer ??? ????? ?? ?????? ??? ? ??. ?? undefined ? ??? ????? null ? ?? ??? ???.

??? ?? ??

js
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);

JSON ??? ??? {"week":45,"month":7} ??.

??? ?? ??

replacer ? ??? ??, ? ??? ?? JSON ???? ??? ???? ??? ??? ????.

js
JSON.stringify(foo, ["week", "month"]);
// '{"week":45,"month":7}', ?? "week" ? "month" ??? ????

space ?? ??

space ????? ?? ???? ??? ????. ??? ?? ?? 10? ??? ?? ?? ??? ??????, ???? ?? ?? ??? ?? ?? 10? ?? ???? ??.

js
JSON.stringify({ a: 2 }, null, " ");
// '{
//  "a": 2
// }'

'\t'? ???? ????? ???? ? ?????? ???.

js
JSON.stringify({ uno: 1, dos: 2 }, null, "\t");
// returns the string:
// '{
//     "uno": 1,
//     "dos": 2
// }'

toJSON() ??

If an object being stringified has a property named toJSON whose value is a function, then the toJSON() method customizes JSON stringification behavior: instead of the object being serialized, the value returned by the toJSON() method when called will be serialized. For example:

js
var obj = {
  foo: "foo",
  toJSON: function () {
    return "bar";
  },
};
JSON.stringify(obj); // '"bar"'
JSON.stringify({ x: obj }); // '{"x":"bar"}'

Example of using JSON.stringify() with localStorage

In a case where you want to store an object created by your user and allowing it to be restored even after the browser has been closed, the following example is a model for the applicability of JSON.stringify():

?? : Functions are not a valid JSON data type so they will not work. However, they can be displayed if first converted to a string (e.g. in the replacer), via the function's toString method. Also, some objects like Date will be a string after JSON.parse().

js
// Creating an example of JSON
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 });

// Converting the JSON string with JSON.stringify()
// then saving with localStorage in the name of session
localStorage.setItem("session", JSON.stringify(session));

// Example of how to transform the String generated through
// JSON.stringify() and saved in localStorage in JSON object again
var restoredSession = JSON.parse(localStorage.getItem("session"));

// Now restoredSession variable contains the object that was saved
// in localStorage
console.log(restoredSession);

???

Specification
ECMAScript? 2026 Language?Specification
# sec-json.stringify

???? ???

?? ??