Hướng dẫn which methods convert a json string to a javascript object? - phương pháp nào chuyển đổi một chuỗi json thành một đối tượng javascript?

Phương thức

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
2 chuyển đổi giá trị JavaScript thành chuỗi JSON, tùy chọn thay thế các giá trị nếu hàm thay thế được chỉ định hoặc tùy chọn chỉ bao gồm các thuộc tính được chỉ định nếu một mảng thay thế được chỉ định.
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
2
method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

Thử nó

Cú pháp

JSON.stringify(value)
JSON.stringify(value, replacer)
JSON.stringify(value, replacer, space)

Thông số

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
3

Giá trị để chuyển đổi thành chuỗi JSON.

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4 Tùy chọnOptional

Một hàm làm thay đổi hành vi của quá trình chuỗi hoặc một mảng các chuỗi hoặc số các thuộc tính đặt tên của

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
3 nên được đưa vào đầu ra. Nếu
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4 là một mảng, tất cả các phần tử không phải là chuỗi hoặc số (có thể là nguyên thủy hoặc đối tượng bao bọc), bao gồm các giá trị
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
7, hoàn toàn bị bỏ qua. Nếu
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4 là bất cứ thứ gì khác ngoài hàm hoặc mảng (ví dụ:
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
9 hoặc không được cung cấp), tất cả các thuộc tính của đối tượng đều được bao gồm trong chuỗi JSON kết quả.

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
0 Tùy chọnOptional

Một chuỗi hoặc số được sử dụng để chèn không gian trắng (bao gồm cả thụt lề, ký tự ngắt dòng, v.v.) vào chuỗi JSON đầu ra cho mục đích dễ đọc.

Nếu đây là một số, nó chỉ ra số lượng ký tự không gian được sử dụng làm thụt, kẹp thành 10 (nghĩa là, bất kỳ số nào lớn hơn

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
1 được coi là
function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
1). Giá trị nhỏ hơn 1 chỉ ra rằng không nên sử dụng không gian.

Nếu đây là một chuỗi, chuỗi (hoặc 10 ký tự đầu tiên của chuỗi, nếu nó dài hơn thế) sẽ được chèn trước mỗi đối tượng hoặc mảng lồng nhau.

Nếu

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
0 là bất cứ thứ gì khác ngoài chuỗi hoặc số (có thể là một đối tượng nguyên thủy hoặc bao bọc) - ví dụ, là
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
9 hoặc không được cung cấp - không có không gian trắng được sử dụng.

Giá trị trả về

Một chuỗi JSON đại diện cho giá trị đã cho hoặc không xác định.

Ngoại lệ

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
5

Ném nếu một trong những điều sau đây là đúng:

  • JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
    // '"2006-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // 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" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    3 chứa một tham chiếu tròn.
  • Một giá trị
    function replacer(key, value) {
      // Filtering out properties
      if (typeof value === "string") {
        return undefined;
      }
      return value;
    }
    
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    JSON.stringify(foo, replacer);
    // '{"week":45,"month":7}'
    
    7 gặp phải.

Sự mô tả

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
2 Chuyển đổi giá trị thành ký hiệu JSON đại diện cho nó:

  • function replacer(key, value) {
      // Filtering out properties
      if (typeof value === "string") {
        return undefined;
      }
      return value;
    }
    
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    JSON.stringify(foo, replacer);
    // '{"week":45,"month":7}'
    
    9,
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    0,
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    1 và
    function replacer(key, value) {
      // Filtering out properties
      if (typeof value === "string") {
        return undefined;
      }
      return value;
    }
    
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    JSON.stringify(foo, replacer);
    // '{"week":45,"month":7}'
    
    7 (có thể đạt được thông qua
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    3) được chuyển đổi thành các giá trị nguyên thủy tương ứng trong quá trình chuỗi, theo ngữ nghĩa chuyển đổi truyền thống. Các đối tượng
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
    // '"2006-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // 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" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    7 (có thể đạt được thông qua
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    3) được coi là đối tượng đơn giản.
  • Cố gắng tuần tự hóa các giá trị
    function replacer(key, value) {
      // Filtering out properties
      if (typeof value === "string") {
        return undefined;
      }
      return value;
    }
    
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    JSON.stringify(foo, replacer);
    // '{"week":45,"month":7}'
    
    7 sẽ ném. Tuy nhiên, nếu Bigint có phương pháp
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    7 (thông qua việc điếm:
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    8), phương pháp đó có thể cung cấp kết quả tuần tự hóa. Hạn chế này đảm bảo rằng hành vi tuần tự hóa thích hợp (và, rất có thể, hành vi giảm dần đi kèm của nó) luôn được người dùng cung cấp rõ ràng.
  • Các giá trị
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    9,
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    0 và
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
    // '"2006-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // 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" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    7 không phải là giá trị JSON hợp lệ. Nếu bất kỳ giá trị nào như vậy gặp phải trong quá trình chuyển đổi, chúng sẽ bị bỏ qua (khi được tìm thấy trong một đối tượng) hoặc thay đổi thành
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
    // '"2006-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // 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" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    9 (khi được tìm thấy trong một mảng).
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
    // '"2006-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // 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" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    2 có thể trả về
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    9 khi chuyển các giá trị "thuần túy" như
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    5 hoặc
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    6.
  • Các số
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    7 và
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    8, cũng như giá trị
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
    // '"2006-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // 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" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    9, đều được coi là
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
    // '"2006-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // 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" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    9. (Nhưng không giống như các giá trị ở điểm trước, chúng sẽ không bao giờ bị bỏ qua.)
  • Mảng được nối tiếp như các mảng (được đặt bởi giá đỡ vuông). Chỉ các chỉ số mảng giữa 0 đến
    console.log(JSON.stringify({ a: 2 }, null, " "));
    /*
    {
     "a": 2
    }
    */
    
    1 (bao gồm) được tuần tự hóa; Các thuộc tính khác bị bỏ qua.
  • Đối với các đối tượng khác:
    • Tất cả các thuộc tính ________ 17 sẽ bị bỏ qua hoàn toàn, ngay cả khi sử dụng tham số
      JSON.stringify({}); // '{}'
      JSON.stringify(true); // 'true'
      JSON.stringify("foo"); // '"foo"'
      JSON.stringify([1, "false", false]); // '[1,"false",false]'
      JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
      JSON.stringify({ x: 5 }); // '{"x":5}'
      
      JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
      // '"2006-01-02T15:04:05.000Z"'
      
      JSON.stringify({ x: 5, y: 6 });
      // '{"x":5,"y":6}'
      JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
      // '[3,"false",false]'
      
      // String-keyed array elements are not enumerable and make no sense in JSON
      const a = ["foo", "bar"];
      a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
      JSON.stringify(a);
      // '["foo","bar"]'
      
      JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
      // '{"x":[10,null,null,null]}'
      
      // Standard data structures
      JSON.stringify([
        new Set([1]),
        new Map([[1, 2]]),
        new WeakSet([{ a: 1 }]),
        new WeakMap([[{ a: 1 }, 2]]),
      ]);
      // '[{},{},{},{}]'
      
      // TypedArray
      JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
      // '[{"0":1},{"0":1},{"0":1}]'
      JSON.stringify([
        new Uint8Array([1]),
        new Uint8ClampedArray([1]),
        new Uint16Array([1]),
        new Uint32Array([1]),
      ]);
      // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
      JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
      // '[{"0":1},{"0":1}]'
      
      // toJSON()
      JSON.stringify({
        x: 5,
        y: 6,
        toJSON() {
          return this.x + this.y;
        },
      });
      // '11'
      
      // 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" }, (k, v) => {
        if (typeof k === "symbol") {
          return "a symbol";
        }
      });
      // undefined
      
      // Non-enumerable properties:
      JSON.stringify(
        Object.create(null, {
          x: { value: "x", enumerable: false },
          y: { value: "y", enumerable: true },
        }),
      );
      // '{"y":"y"}'
      
      // BigInt values throw
      JSON.stringify({ x: 2n });
      // TypeError: BigInt value can't be serialized in JSON
      
      4.
    • Nếu giá trị có phương thức
      function makeReplacer() {
        let isInitial = true;
      
        return (key, value) => {
          if (isInitial) {
            isInitial = false;
            return value;
          }
          if (key === "") {
            // Omit all properties with name "" (except the initial object)
            return undefined;
          }
          return value;
        };
      }
      
      const replacer = makeReplacer();
      console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
      
      7, thì chịu trách nhiệm xác định dữ liệu nào sẽ được tuần tự hóa. Thay vì đối tượng được tuần tự hóa, giá trị được trả về bằng phương pháp
      function makeReplacer() {
        let isInitial = true;
      
        return (key, value) => {
          if (isInitial) {
            isInitial = false;
            return value;
          }
          if (key === "") {
            // Omit all properties with name "" (except the initial object)
            return undefined;
          }
          return value;
        };
      }
      
      const replacer = makeReplacer();
      console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
      
      7 khi được gọi sẽ được tuần tự hóa.
      JSON.stringify({}); // '{}'
      JSON.stringify(true); // 'true'
      JSON.stringify("foo"); // '"foo"'
      JSON.stringify([1, "false", false]); // '[1,"false",false]'
      JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
      JSON.stringify({ x: 5 }); // '{"x":5}'
      
      JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
      // '"2006-01-02T15:04:05.000Z"'
      
      JSON.stringify({ x: 5, y: 6 });
      // '{"x":5,"y":6}'
      JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
      // '[3,"false",false]'
      
      // String-keyed array elements are not enumerable and make no sense in JSON
      const a = ["foo", "bar"];
      a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
      JSON.stringify(a);
      // '["foo","bar"]'
      
      JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
      // '{"x":[10,null,null,null]}'
      
      // Standard data structures
      JSON.stringify([
        new Set([1]),
        new Map([[1, 2]]),
        new WeakSet([{ a: 1 }]),
        new WeakMap([[{ a: 1 }, 2]]),
      ]);
      // '[{},{},{},{}]'
      
      // TypedArray
      JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
      // '[{"0":1},{"0":1},{"0":1}]'
      JSON.stringify([
        new Uint8Array([1]),
        new Uint8ClampedArray([1]),
        new Uint16Array([1]),
        new Uint32Array([1]),
      ]);
      // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
      JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
      // '[{"0":1},{"0":1}]'
      
      // toJSON()
      JSON.stringify({
        x: 5,
        y: 6,
        toJSON() {
          return this.x + this.y;
        },
      });
      // '11'
      
      // 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" }, (k, v) => {
        if (typeof k === "symbol") {
          return "a symbol";
        }
      });
      // undefined
      
      // Non-enumerable properties:
      JSON.stringify(
        Object.create(null, {
          x: { value: "x", enumerable: false },
          y: { value: "y", enumerable: true },
        }),
      );
      // '{"y":"y"}'
      
      // BigInt values throw
      JSON.stringify({ x: 2n });
      // TypeError: BigInt value can't be serialized in JSON
      
      2 gọi
      console.log(JSON.stringify({ a: 2 }, null, " "));
      /*
      {
       "a": 2
      }
      */
      
      7 với một tham số,
      console.log(JSON.stringify({ a: 2 }, null, " "));
      /*
      {
       "a": 2
      }
      */
      
      8, có cùng ngữ nghĩa với tham số
      console.log(JSON.stringify({ a: 2 }, null, " "));
      /*
      {
       "a": 2
      }
      */
      
      8 của hàm
      JSON.stringify({}); // '{}'
      JSON.stringify(true); // 'true'
      JSON.stringify("foo"); // '"foo"'
      JSON.stringify([1, "false", false]); // '[1,"false",false]'
      JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
      JSON.stringify({ x: 5 }); // '{"x":5}'
      
      JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
      // '"2006-01-02T15:04:05.000Z"'
      
      JSON.stringify({ x: 5, y: 6 });
      // '{"x":5,"y":6}'
      JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
      // '[3,"false",false]'
      
      // String-keyed array elements are not enumerable and make no sense in JSON
      const a = ["foo", "bar"];
      a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
      JSON.stringify(a);
      // '["foo","bar"]'
      
      JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
      // '{"x":[10,null,null,null]}'
      
      // Standard data structures
      JSON.stringify([
        new Set([1]),
        new Map([[1, 2]]),
        new WeakSet([{ a: 1 }]),
        new WeakMap([[{ a: 1 }, 2]]),
      ]);
      // '[{},{},{},{}]'
      
      // TypedArray
      JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
      // '[{"0":1},{"0":1},{"0":1}]'
      JSON.stringify([
        new Uint8Array([1]),
        new Uint8ClampedArray([1]),
        new Uint16Array([1]),
        new Uint32Array([1]),
      ]);
      // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
      JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
      // '[{"0":1},{"0":1}]'
      
      // toJSON()
      JSON.stringify({
        x: 5,
        y: 6,
        toJSON() {
          return this.x + this.y;
        },
      });
      // '11'
      
      // 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" }, (k, v) => {
        if (typeof k === "symbol") {
          return "a symbol";
        }
      });
      // undefined
      
      // Non-enumerable properties:
      JSON.stringify(
        Object.create(null, {
          x: { value: "x", enumerable: false },
          y: { value: "y", enumerable: true },
        }),
      );
      // '{"y":"y"}'
      
      // BigInt values throw
      JSON.stringify({ x: 2n });
      // TypeError: BigInt value can't be serialized in JSON
      
      4:
      • Nếu đối tượng này là giá trị thuộc tính, tên thuộc tính
      • Nếu nó nằm trong một mảng, chỉ mục trong mảng, dưới dạng chuỗi
      • Nếu
        JSON.stringify({}); // '{}'
        JSON.stringify(true); // 'true'
        JSON.stringify("foo"); // '"foo"'
        JSON.stringify([1, "false", false]); // '[1,"false",false]'
        JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
        JSON.stringify({ x: 5 }); // '{"x":5}'
        
        JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
        // '"2006-01-02T15:04:05.000Z"'
        
        JSON.stringify({ x: 5, y: 6 });
        // '{"x":5,"y":6}'
        JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
        // '[3,"false",false]'
        
        // String-keyed array elements are not enumerable and make no sense in JSON
        const a = ["foo", "bar"];
        a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
        JSON.stringify(a);
        // '["foo","bar"]'
        
        JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
        // '{"x":[10,null,null,null]}'
        
        // Standard data structures
        JSON.stringify([
          new Set([1]),
          new Map([[1, 2]]),
          new WeakSet([{ a: 1 }]),
          new WeakMap([[{ a: 1 }, 2]]),
        ]);
        // '[{},{},{},{}]'
        
        // TypedArray
        JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
        // '[{"0":1},{"0":1},{"0":1}]'
        JSON.stringify([
          new Uint8Array([1]),
          new Uint8ClampedArray([1]),
          new Uint16Array([1]),
          new Uint32Array([1]),
        ]);
        // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
        JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
        // '[{"0":1},{"0":1}]'
        
        // toJSON()
        JSON.stringify({
          x: 5,
          y: 6,
          toJSON() {
            return this.x + this.y;
          },
        });
        // '11'
        
        // 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" }, (k, v) => {
          if (typeof k === "symbol") {
            return "a symbol";
          }
        });
        // undefined
        
        // Non-enumerable properties:
        JSON.stringify(
          Object.create(null, {
            x: { value: "x", enumerable: false },
            y: { value: "y", enumerable: true },
          }),
        );
        // '{"y":"y"}'
        
        // BigInt values throw
        JSON.stringify({ x: 2n });
        // TypeError: BigInt value can't be serialized in JSON
        
        2 được gọi trực tiếp trên đối tượng này, một chuỗi trống
      Các đối tượng
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      2 Thực hiện phương thức
      function makeReplacer() {
        let isInitial = true;
      
        return (key, value) => {
          if (isInitial) {
            isInitial = false;
            return value;
          }
          if (key === "") {
            // Omit all properties with name "" (except the initial object)
            return undefined;
          }
          return value;
        };
      }
      
      const replacer = makeReplacer();
      console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
      
      7 trả về một chuỗi (giống như
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      4). Vì vậy, chúng sẽ được xâu chuỗi là chuỗi.
    • Chỉ có các tài sản riêng được truy cập. Điều này có nghĩa là
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      5,
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      6, v.v. sẽ trở thành
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      7. Bạn có thể sử dụng tham số
      JSON.stringify({}); // '{}'
      JSON.stringify(true); // 'true'
      JSON.stringify("foo"); // '"foo"'
      JSON.stringify([1, "false", false]); // '[1,"false",false]'
      JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
      JSON.stringify({ x: 5 }); // '{"x":5}'
      
      JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
      // '"2006-01-02T15:04:05.000Z"'
      
      JSON.stringify({ x: 5, y: 6 });
      // '{"x":5,"y":6}'
      JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
      // '[3,"false",false]'
      
      // String-keyed array elements are not enumerable and make no sense in JSON
      const a = ["foo", "bar"];
      a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
      JSON.stringify(a);
      // '["foo","bar"]'
      
      JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
      // '{"x":[10,null,null,null]}'
      
      // Standard data structures
      JSON.stringify([
        new Set([1]),
        new Map([[1, 2]]),
        new WeakSet([{ a: 1 }]),
        new WeakMap([[{ a: 1 }, 2]]),
      ]);
      // '[{},{},{},{}]'
      
      // TypedArray
      JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
      // '[{"0":1},{"0":1},{"0":1}]'
      JSON.stringify([
        new Uint8Array([1]),
        new Uint8ClampedArray([1]),
        new Uint16Array([1]),
        new Uint32Array([1]),
      ]);
      // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
      JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
      // '[{"0":1},{"0":1}]'
      
      // toJSON()
      JSON.stringify({
        x: 5,
        y: 6,
        toJSON() {
          return this.x + this.y;
        },
      });
      // '11'
      
      // 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" }, (k, v) => {
        if (typeof k === "symbol") {
          return "a symbol";
        }
      });
      // undefined
      
      // Non-enumerable properties:
      JSON.stringify(
        Object.create(null, {
          x: { value: "x", enumerable: false },
          y: { value: "y", enumerable: true },
        }),
      );
      // '{"y":"y"}'
      
      // BigInt values throw
      JSON.stringify({ x: 2n });
      // TypeError: BigInt value can't be serialized in JSON
      
      4 để tuần tự hóa chúng thành một cái gì đó hữu ích hơn. Các thuộc tính được truy cập bằng thuật toán tương tự như
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      9, có thứ tự được xác định rõ và ổn định trong các triển khai. Ví dụ:
      const obj = {
        data: "data",
      
        toJSON(key) {
          return key ? `Now I am a nested object under key '${key}'` : this;
        },
      };
      
      JSON.stringify(obj);
      // '{"data":"data"}'
      
      JSON.stringify({ obj });
      // '{"obj":"Now I am a nested object under key 'obj'"}'
      
      JSON.stringify([obj]);
      // '["Now I am a nested object under key '0'"]'
      
      0 trên cùng một đối tượng sẽ luôn tạo ra cùng một chuỗi và
      const obj = {
        data: "data",
      
        toJSON(key) {
          return key ? `Now I am a nested object under key '${key}'` : this;
        },
      };
      
      JSON.stringify(obj);
      // '{"data":"data"}'
      
      JSON.stringify({ obj });
      // '{"obj":"Now I am a nested object under key 'obj'"}'
      
      JSON.stringify([obj]);
      // '["Now I am a nested object under key '0'"]'
      
      1 sẽ tạo ra một đối tượng có cùng thứ tự chính như bản gốc (giả sử đối tượng hoàn toàn có thể sử dụng được).

Tham số thay thế

Tham số

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4 có thể là một hàm hoặc một mảng.

Là một mảng, các phần tử của nó cho biết tên của các thuộc tính trong đối tượng nên được bao gồm trong chuỗi JSON kết quả. Chỉ các giá trị chuỗi và số được tính đến; Các phím biểu tượng bị bỏ qua.

Là một hàm, phải mất hai tham số:

console.log(JSON.stringify({ a: 2 }, null, " "));
/*
{
 "a": 2
}
*/
8 và
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
3 được xâu chuỗi. Đối tượng mà khóa được tìm thấy được cung cấp dưới dạng bối cảnh
const obj = {
  data: "data",

  toJSON(key) {
    return key ? `Now I am a nested object under key '${key}'` : this;
  },
};

JSON.stringify(obj);
// '{"data":"data"}'

JSON.stringify({ obj });
// '{"obj":"Now I am a nested object under key 'obj'"}'

JSON.stringify([obj]);
// '["Now I am a nested object under key '0'"]'
6 của ____ 14.

Hàm

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4 cũng được gọi cho đối tượng ban đầu cũng được xâu chuỗi, trong trường hợp đó
console.log(JSON.stringify({ a: 2 }, null, " "));
/*
{
 "a": 2
}
*/
8 là một chuỗi trống (
const obj = {
  data: "data",

  toJSON(key) {
    return key ? `Now I am a nested object under key '${key}'` : this;
  },
};

JSON.stringify(obj);
// '{"data":"data"}'

JSON.stringify({ obj });
// '{"obj":"Now I am a nested object under key 'obj'"}'

JSON.stringify([obj]);
// '["Now I am a nested object under key '0'"]'
9). Sau đó, nó được gọi cho mỗi thuộc tính trên đối tượng hoặc mảng đang được xâu chuỗi. Các chỉ số mảng sẽ được cung cấp ở dạng chuỗi của nó là
console.log(JSON.stringify({ a: 2 }, null, " "));
/*
{
 "a": 2
}
*/
8. Giá trị thuộc tính hiện tại sẽ được thay thế bằng giá trị trả về của ____ 14 để xâu chuỗi. Điều này có nghĩa là:

  • Nếu bạn trả về một số, chuỗi, boolean hoặc
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
    // '"2006-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // 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" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    9, giá trị đó được tuần tự hóa trực tiếp và được sử dụng làm giá trị của thuộc tính. (Trả lại một Bigint cũng sẽ ném.)
  • Nếu bạn trả lại
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    0,
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
    // '"2006-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // 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" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    7 hoặc
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    9, tài sản không được bao gồm trong đầu ra.
  • Nếu bạn trả về bất kỳ đối tượng nào khác, đối tượng được xâu chuỗi đệ quy, gọi hàm
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
    // '"2006-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // 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" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    4 trên mỗi thuộc tính.

Lưu ý: Khi phân tích cú pháp JSON được tạo bằng các hàm

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4, bạn có thể muốn sử dụng tham số
const circularReference = {};
circularReference.myself = circularReference;

// Serializing circular references throws "TypeError: cyclic object value"
JSON.stringify(circularReference);
8 để thực hiện thao tác ngược.
When parsing JSON generated with
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4 functions, you would likely want to use the
const circularReference = {};
circularReference.myself = circularReference;

// Serializing circular references throws "TypeError: cyclic object value"
JSON.stringify(circularReference);
8 parameter to perform the reverse operation.

Thông thường, chỉ mục của các phần tử mảng sẽ không bao giờ thay đổi (ngay cả khi phần tử là một giá trị không hợp lệ như hàm, nó sẽ trở thành

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
9 thay vì bị bỏ qua). Sử dụng chức năng
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4 cho phép bạn kiểm soát thứ tự của các phần tử mảng bằng cách trả về một mảng khác.

Tham số không gian

Tham số

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
0 có thể được sử dụng để kiểm soát khoảng cách trong chuỗi cuối cùng.

  • Nếu đó là một con số, các cấp độ liên tiếp trong chuỗi chuỗi sẽ được thụt vào bởi nhiều ký tự không gian này.
  • Nếu đó là một chuỗi, các cấp độ liên tiếp sẽ được thụt vào chuỗi này.

Mỗi cấp độ thụt sẽ không bao giờ dài hơn 10. Giá trị số của

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
0 được kẹp thành 10 và các giá trị chuỗi bị cắt xuống còn 10 ký tự.

Ví dụ

Sử dụng JSON.Stringify

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON

Sử dụng một chức năng như người thay thế

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'

Nếu bạn muốn

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4 phân biệt một đối tượng ban đầu với một phím với một thuộc tính chuỗi trống (vì cả hai sẽ cho chuỗi trống làm khóa và có khả năng là một đối tượng là giá trị), bạn sẽ phải theo dõi số lần lặp (nếu đó là Ngoài lần lặp đầu tiên, nó là một khóa chuỗi trống chính hãng).

function makeReplacer() {
  let isInitial = true;

  return (key, value) => {
    if (isInitial) {
      isInitial = false;
      return value;
    }
    if (key === "") {
      // Omit all properties with name "" (except the initial object)
      return undefined;
    }
    return value;
  };
}

const replacer = makeReplacer();
console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"

Sử dụng một mảng làm người thay thế

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};

JSON.stringify(foo, ["week", "month"]);
// '{"week":45,"month":7}', only keep "week" and "month" properties

Sử dụng tham số không gian

Thụt đầu ra với một không gian:

console.log(JSON.stringify({ a: 2 }, null, " "));
/*
{
 "a": 2
}
*/

Sử dụng một ký tự tab bắt chước tiêu chuẩn xuất hiện in ấn đẹp mắt:

console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
/*
{
	"uno": 1,
	"dos": 2
}
*/

hành vi tojson ()

Xác định

function makeReplacer() {
  let isInitial = true;

  return (key, value) => {
    if (isInitial) {
      isInitial = false;
      return value;
    }
    if (key === "") {
      // Omit all properties with name "" (except the initial object)
      return undefined;
    }
    return value;
  };
}

const replacer = makeReplacer();
console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
7 cho một đối tượng cho phép ghi đè hành vi tuần tự hóa của nó.

const obj = {
  data: "data",

  toJSON(key) {
    return key ? `Now I am a nested object under key '${key}'` : this;
  },
};

JSON.stringify(obj);
// '{"data":"data"}'

JSON.stringify({ obj });
// '{"obj":"Now I am a nested object under key 'obj'"}'

JSON.stringify([obj]);
// '["Now I am a nested object under key '0'"]'

Vấn đề với các tài liệu tham khảo tuần hoàn tròn

Vì định dạng JSON không hỗ trợ các tài liệu tham khảo đối tượng (mặc dù dự thảo IETF tồn tại), một

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
5 sẽ được ném nếu một người cố gắng mã hóa một đối tượng có tham chiếu tròn.

const circularReference = {};
circularReference.myself = circularReference;

// Serializing circular references throws "TypeError: cyclic object value"
JSON.stringify(circularReference);

Để tuần tự hóa các tài liệu tham khảo tròn, bạn có thể sử dụng thư viện hỗ trợ chúng (ví dụ: Chu kỳ.js của Douglas Crockford) hoặc tự mình thực hiện giải pháp, sẽ yêu cầu tìm và thay thế (hoặc xóa) các tài liệu tham khảo theo chu kỳ bằng các giá trị có thể nối tiếp.

Nếu bạn đang sử dụng

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
2 cho một đối tượng sao chép sâu, thay vào đó bạn có thể muốn sử dụng
// Creating an example of JSON
const 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
const restoredSession = JSON.parse(localStorage.getItem("session"));

// Now restoredSession variable contains the object that was saved
// in localStorage
console.log(restoredSession);
7, hỗ trợ các tài liệu tham khảo tròn. API động cơ JavaScript để tuần tự hóa nhị phân, chẳng hạn như
// Creating an example of JSON
const 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
const restoredSession = JSON.parse(localStorage.getItem("session"));

// Now restoredSession variable contains the object that was saved
// in localStorage
console.log(restoredSession);
8, cũng hỗ trợ các tài liệu tham khảo tròn.

Sử dụng json.Stringify () với localStorage

Trong trường hợp bạn muốn lưu trữ một đối tượng do người dùng của bạn tạo và cho phép nó được khôi phục ngay cả sau khi trình duyệt đã được đóng, ví dụ sau đây là mô hình cho khả năng áp dụng của

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
2:

// Creating an example of JSON
const 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
const restoredSession = JSON.parse(localStorage.getItem("session"));

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

JSON được hình thành tốt.Stringify ()

Các động cơ thực hiện JSON được hình thành tốt. Đặc điểm kỹ thuật sẽ xâu chuỗi các chất thay thế đơn độc (bất kỳ điểm mã nào từ U+D800 đến U+DFFF) bằng cách sử dụng trình tự thoát Unicode thay vì theo nghĩa đen (xuất ra các chất thay thế đơn độc). Trước khi thay đổi này, các chuỗi như vậy không thể được mã hóa trong UTF-8 hoặc UTF-16 hợp lệ:

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
0

Nhưng với sự thay đổi này

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
2 thể hiện các chất thay thế đơn độc bằng cách sử dụng các chuỗi thoát JSON có thể được mã hóa trong UTF-8 hoặc UTF-16 hợp lệ:

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
1

Sự thay đổi này sẽ tương thích ngược miễn là bạn chuyển kết quả của

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
2 cho các API như
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
02 sẽ chấp nhận bất kỳ văn bản JSON hợp lệ nào, bởi vì họ sẽ đối xử với các chất thay thế đơn độc của Unicode giống hệt với chính người thay thế đơn độc. Chỉ khi bạn trực tiếp giải thích kết quả của
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'

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

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

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

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// 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" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
2, bạn mới cần xử lý cẩn thận hai mã hóa có thể có của các điểm mã này.

Thông số kỹ thuật

Sự chỉ rõ
Đặc tả ngôn ngữ Ecmascript # Sec-Json.Stringify
# sec-json.stringify

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm

Phương pháp nào được sử dụng để chuyển đổi dữ liệu JSON thành đối tượng?

Văn bản/đối tượng JSON có thể được chuyển đổi thành đối tượng JavaScript bằng hàm json.parse ().JSON. parse().

Làm cách nào để chuyển đổi tệp JSON thành một đối tượng?

Sử dụng hàm javascript json.parse () để chuyển đổi văn bản thành đối tượng javascript: const obj = json.parse ('{"name": "john", "tuổi": 30, "thành phố": "new york"}');Hãy chắc chắn rằng văn bản ở định dạng JSON, nếu không bạn sẽ gặp lỗi cú pháp.: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

Phương pháp nào chuyển đổi chuỗi JSON?

Phương thức json.Stringify () chuyển đổi giá trị javascript thành chuỗi JSON, tùy chọn thay thế các giá trị nếu hàm thay thế được chỉ định hoặc tùy chọn chỉ bao gồm các thuộc tính được chỉ định nếu một mảng thay thế được chỉ định.JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

Làm thế nào để bạn chuyển đổi JSON sang đối tượng Java?

Chúng ta có thể chuyển đổi một JSON sang đối tượng Java bằng phương thức readValue () của lớp objectMapper, phương thức này thu hút nội dung JSON từ chuỗi nội dung JSON đã cho.using the readValue() method of ObjectMapper class, this method deserializes a JSON content from given JSON content String.