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

John là MVP của Microsoft, đồng thời là nhà tư vấn và huấn luyện viên tự do chuyên về Excel, Power BI, Power Automate, Power Apps và SharePoint. Bạn có thể tìm thấy các bài viết thú vị khác của John trên blog hoặc kênh YouTube của anh ấy

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[1906, 0, 2, 15, 4, 5]];
// '"1906-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 ý 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 đã chỉ định nếu mảng thay thế được chỉ định

JSON.stringify[value]
JSON.stringify[value, replacer]
JSON.stringify[value, replacer, space]

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[1906, 0, 2, 15, 4, 5]];
// '"1906-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[1906, 0, 2, 15, 4, 5]];
// '"1906-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ọn

Một hàm thay đổi hành vi của quá trình xâu chuỗi hóa hoặc một mảng các chuỗi và số chỉ định các thuộc tính 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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 sẽ đượ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[1906, 0, 2, 15, 4, 5]];
// '"1906-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, thì tất cả các phần tử trong mảng này không phải là chuỗi hoặc số [đối tượng nguyên thủy hoặc đối tượng bao bọc], bao gồm 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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, đều bị bỏ qua hoàn toàn. 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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 kỳ thứ gì khác ngoài một hàm hoặc một mảng [e. g.
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[1906, 0, 2, 15, 4, 5]];
// '"1906-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ó khóa chuỗi của đối tượng đượ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ọn

Một chuỗi hoặc số được sử dụng để chèn khoảng trắng [bao gồm thụt đầu dòng, 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ó cho biết số lượng ký tự khoảng trắng được sử dụng làm thụt đầu dòng, được kẹp vào 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 đều được coi như 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]. Các giá trị nhỏ hơn 1 cho biết không nên sử dụng khoảng trắng

Nếu đây là một chuỗi, thì chuỗi đó [hoặc 10 ký tự đầu tiên của chuỗi, nếu dài hơn chuỗi đó] sẽ được chèn vào 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 kỳ thứ gì khác ngoài chuỗi hoặc số [có thể là đối tượng nguyên thủy hoặc đối tượng 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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ó khoảng trắng nào được sử dụng

Chuỗi JSON đại diện cho giá trị đã cho hoặc không xác định

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

Bị 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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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 tham chiếu vòng
  • Gặp phải 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

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[1906, 0, 2, 15, 4, 5]];
// '"1906-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 một giá trị thành ký hiệu JSON đại diện cho nó

  • Các đối tượng
    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,
    JSON.stringify[value]
    JSON.stringify[value, replacer]
    JSON.stringify[value, replacer, space]
    
    90,
    JSON.stringify[value]
    JSON.stringify[value, replacer]
    JSON.stringify[value, replacer, space]
    
    91 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ể nhận được qua
    JSON.stringify[value]
    JSON.stringify[value, replacer]
    JSON.stringify[value, replacer, space]
    
    93] được chuyển đổi thành các giá trị nguyên thủy tương ứng trong quá trình xâu chuỗi hóa, phù hợp với 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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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ể nhận được qua
    JSON.stringify[value]
    JSON.stringify[value, replacer]
    JSON.stringify[value, replacer, space]
    
    93] đượ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
    JSON.stringify[value]
    JSON.stringify[value, replacer]
    JSON.stringify[value, replacer, space]
    
    97 [thông qua Monkeypatching.
    JSON.stringify[value]
    JSON.stringify[value, replacer]
    JSON.stringify[value, replacer, space]
    
    98], phương pháp đó có thể cung cấp kết quả lập số sê-ri. Ràng buộc này đảm bảo rằng hành vi tuần tự hóa thích hợp [và rất có thể là giải tuần tự hóa đi kèm] luôn được cung cấp rõ ràng bởi người dùng
  • Giá trị
    JSON.stringify[value]
    JSON.stringify[value, replacer]
    JSON.stringify[value, replacer, space]
    
    99,
    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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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
    
    90 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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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 được đổ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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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ề
    JSON.stringify[value]
    JSON.stringify[value, replacer]
    JSON.stringify[value, replacer, space]
    
    99 khi chuyển các giá trị "thuần túy" 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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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
    
    95 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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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
    
    96
  • Các 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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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
    
    97 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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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
    
    98, 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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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 sắp xếp theo thứ tự dưới dạng mảng [được đặt trong dấu ngoặc vuông]. Chỉ các chỉ số mảng từ 0 đế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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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
    
    01 [bao gồm] được đánh số thứ tự;
  • Đối với các đối tượng khác
    • Tất cả các thuộc tính có khó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[1906, 0, 2, 15, 4, 5]];
      // '"1906-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 sẽ bị bỏ qua hoàn toàn, ngay cả khi sử dụng tham số
    • Nếu giá trị có phương thức
      JSON.stringify[value]
      JSON.stringify[value, replacer]
      JSON.stringify[value, replacer, space]
      
      97, nó có trách nhiệm xác định dữ liệu nào sẽ được sắp xếp theo thứ tự. Thay vì đối tượng được đánh số thứ tự, giá trị được trả về bởi phương thức
      JSON.stringify[value]
      JSON.stringify[value, replacer]
      JSON.stringify[value, replacer, space]
      
      97 khi được gọi sẽ được đánh số thứ 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[1906, 0, 2, 15, 4, 5]];
      // '"1906-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
      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[1906, 0, 2, 15, 4, 5]];
      // '"1906-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
      
      07 với một 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[1906, 0, 2, 15, 4, 5]];
      // '"1906-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
      
      08, có cùng ngữ nghĩa với 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[1906, 0, 2, 15, 4, 5]];
      // '"1906-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
      
      08 của hàm
      • nếu đối tượng này là một giá trị thuộc tính, tên thuộc tính
      • nếu nó ở trong một mảng, thì chỉ mục trong mảng, dưới dạng một 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[1906, 0, 2, 15, 4, 5]];
        // '"1906-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 rỗ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[1906, 0, 2, 15, 4, 5]];
      // '"1906-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
      
      92 triển khai phương thức
      JSON.stringify[value]
      JSON.stringify[value, replacer]
      JSON.stringify[value, replacer, space]
      
      97 trả về một chuỗi [giống 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[1906, 0, 2, 15, 4, 5]];
      // '"1906-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
      
      94]. Do đó, chúng sẽ được xâu thành chuỗi
    • Chỉ vô số thuộc tính riêng được truy cập. Điều này có nghĩa 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[1906, 0, 2, 15, 4, 5]];
      // '"1906-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
      
      95,
      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[1906, 0, 2, 15, 4, 5]];
      // '"1906-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
      
      96, v.v. 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[1906, 0, 2, 15, 4, 5]];
      // '"1906-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
      
      97. Bạn có thể sử dụng tham số để tuần tự hóa chúng thành thứ 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ư
      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[1906, 0, 2, 15, 4, 5]];
      // '"1906-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
      
      99, có thứ tự được xác định rõ ràng và ổn định trong quá trình triển khai. 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[1906, 0, 2, 15, 4, 5]];
      // '"1906-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
      
      60 trên cùng một đối tượng sẽ luôn tạo ra cùng một chuỗi 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[1906, 0, 2, 15, 4, 5]];
      // '"1906-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
      
      61 sẽ tạo ra một đối tượng có cùng thứ tự khóa như ban đầu [giả sử đối tượng hoàn toàn có thể tuần tự hóa JSON]

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[1906, 0, 2, 15, 4, 5]];
// '"1906-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à hàm hoặc 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 sẽ được đưa vào chuỗi JSON kết quả. Chỉ các giá trị chuỗi và số được tính đến;

Là một hàm, nó nhận hai 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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
08 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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 tìm thấy khóa được cung cấp dưới dạng ngữ cả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[1906, 0, 2, 15, 4, 5]];
// '"1906-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
66 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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

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[1906, 0, 2, 15, 4, 5]];
// '"1906-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 xâu chuỗi, trong trường hợp đó,
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[1906, 0, 2, 15, 4, 5]];
// '"1906-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
08 là một chuỗi rỗ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[1906, 0, 2, 15, 4, 5]];
// '"1906-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
69]. Sau đó, nó được gọi cho từng thuộc tính trên đối tượng hoặc mảng được xâu chuỗi. Các chỉ số mảng sẽ được cung cấp ở dạng chuỗi dưới 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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
08. Giá trị thuộc tính hiện tại sẽ được thay thế bằng giá trị trả về 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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 để 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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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ị đó sẽ được đánh số thứ tự trực tiếp và được sử dụng làm giá trị của thuộc tính. [Trả lại BigInt cũng sẽ ném. ]
  • Nếu bạn trả lại 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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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
    
    90,
    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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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
    JSON.stringify[value]
    JSON.stringify[value, replacer]
    JSON.stringify[value, replacer, space]
    
    99, thuộc tính 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, thì đối tượng đó sẽ đượ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[1906, 0, 2, 15, 4, 5]];
    // '"1906-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

Ghi chú. Khi phân tích cú pháp JSON được tạo bằng 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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ố để thực hiện thao tác đảo ngược

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ư một 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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ố

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 số, thì mỗi mức liên tiếp trong quá trình xâu chuỗi sẽ được thụt vào bởi nhiều ký tự khoảng trắng này
  • Nếu là một chuỗi, các mức liên tiếp sẽ được thụt vào bởi chuỗi này

Mỗi cấp độ thụt đầu dòng 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à giá trị chuỗi được cắt bớt thành 10 ký 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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

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[1906, 0, 2, 15, 4, 5]];
// '"1906-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 khóa có thuộc tính chuỗi trống [vì cả hai sẽ cung cấp chuỗi trống làm khóa và có khả năng là một đối tượng làm giá trị], bạn sẽ phải theo dõi số lần lặp lại [nếu nó là

JSON.stringify[value]
JSON.stringify[value, replacer]
JSON.stringify[value, replacer, space]
9

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[1906, 0, 2, 15, 4, 5]];
// '"1906-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

Thụt đầu ra với một dấu cách

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[1906, 0, 2, 15, 4, 5]];
// '"1906-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

Sử dụng một ký tự tab bắt chước hình thức in đẹp tiêu chuẩ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[1906, 0, 2, 15, 4, 5]];
// '"1906-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

Xác định

JSON.stringify[value]
JSON.stringify[value, replacer]
JSON.stringify[value, replacer, space]
97 cho một đối tượng cho phép ghi đè hành vi tuần tự hóa của 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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
6

Vì định dạng JSON không hỗ trợ các tham chiếu đối tượng [mặc dù có tồn tại bản nháp IETF], 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ẽ bị ném nếu một người cố gắng mã hóa một đối tượng bằng các tham chiếu vò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[1906, 0, 2, 15, 4, 5]];
// '"1906-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
8

Để tuần tự hóa các tham chiếu vòng tròn, bạn có thể sử dụng thư viện hỗ trợ chúng [e. g. đi xe đạp. js của Douglas Crockford] hoặc tự triển khai giải pháp, giải pháp này sẽ yêu cầu tìm và thay thế [hoặc xóa] các tham chiếu tuần hoàn bằng các giá trị có thể tuần tự hóa

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[1906, 0, 2, 15, 4, 5]];
// '"1906-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 để sao chép sâu một đối tượng, thay vào đó, bạn có thể muốn sử dụng
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}'
07, hỗ trợ các tham chiếu vòng tròn. API công cụ JavaScript cho tuần tự hóa nhị phân, chẳng hạn như , cũng hỗ trợ tham chiếu vòng tròn

Trong trường hợp bạn muốn lưu trữ một đối tượng được tạo bởi người dùng của bạn và cho phép khôi phục nó ngay cả sau khi đã đóng trình duyệt, ví dụ sau đây là một 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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

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ác công cụ triển khai JSON được định dạng tốt. đặc điểm kỹ thuật stringify sẽ xâu chuỗi các đại diện đơn lẻ [bất kỳ điểm mã nào từ U+D800 đến U+DFFF] bằng cách sử dụng các chuỗi thoát Unicode thay vì theo nghĩa đen [xuất ra các đại diện đơn lẻ]. Trước khi có thay đổi này, các chuỗi như vậy không thể được mã hóa bằng 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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 đại diện cho các đại diện thay thế đơn độc sử dụng các chuỗi thoát JSON có thể được mã hóa bằng 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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

Thay đổi này phải 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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 chẳng hạn 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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ì chúng sẽ coi các lần thoát Unicode của các đại diện đơn lẻ giống hệt với chính các đại diện duy nhất đó. Chỉ khi bạn đang diễn giải trực tiếp 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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ì bạn mới cần xử lý cẩn thận hai cách mã hóa có thể có 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[1906, 0, 2, 15, 4, 5]];
// '"1906-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 đối với các điểm mã này

Đối tượng JSON có thể là một mảng không?

Trong JSON, giá trị mảng phải thuộc loại chuỗi, số, đối tượng, mảng, boolean hoặc null . Trong JavaScript, các giá trị mảng có thể là tất cả các giá trị trên, cộng với bất kỳ biểu thức JavaScript hợp lệ nào khác, bao gồm các hàm, ngày tháng và không xác định.

Làm cách nào để phân tích đối tượng JSON thành mảng trong JavaScript?

Sử dụng JSON. phương thức parse[] để phân tích một mảng JSON , e. g. JSON. phân tích cú pháp [mảng]. Phương thức phân tích một chuỗi JSON và trả về giá trị JavaScript hoặc đối tượng tương đương của nó.

Làm cách nào để chuyển đổi đối tượng đối tượng thành mảng trong JavaScript?

Đối tượng. các phương thức entry[] chuyển đổi các thuộc tính dựa trên chuỗi vô số của đối tượng thành một mảng. .
var superHeroes = {'Captain America'. 1,'Thor'. 8,'Người Sắt'. vô giá trị};
//chuyển đổi các khóa đối tượng thành mảng
var k = Đối tượng. phím [siêu anh hùng];
//chuyển giá trị đối tượng thành mảng
var v = Đối tượng. giá trị [siêu anh hùng];

Tôi có thể lưu trữ JSON trong một mảng không?

Mảng JSON có thể lưu trữ nhiều giá trị . Nó có thể lưu trữ chuỗi, số, boolean hoặc đối tượng trong mảng JSON. Trong mảng JSON, các giá trị phải được phân tách bằng dấu phẩy. [ [dấu ngoặc vuông] đại diện cho mảng JSON.

Chủ Đề