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ỉ địnhJSON.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
3Giá 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ọnMộ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ọnMộ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ắngNế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ụngChuỗ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}'
5Bị ném nếu một trong những điều sau đây là đúng
3 chứa tham chiếu vòngJSON.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
- Gặp phải giá trị
7function 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}'
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
9,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}'
90,JSON.stringify[value] JSON.stringify[value, replacer] JSON.stringify[value, replacer, space]
91 vàJSON.stringify[value] JSON.stringify[value, replacer] JSON.stringify[value, replacer, space]
7 [có thể nhận được quafunction 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}'
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ượngJSON.stringify[value] JSON.stringify[value, replacer] JSON.stringify[value, replacer, space]
7 [có thể nhận được quaJSON.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
93] được coi là đối tượng đơn giảnJSON.stringify[value] JSON.stringify[value, replacer] JSON.stringify[value, replacer, space]
- Cố gắng tuần tự hóa các giá trị
7 sẽ ném. Tuy nhiên, nếu BigInt có phương phápfunction 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}'
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ùngJSON.stringify[value] JSON.stringify[value, replacer] JSON.stringify[value, replacer, space]
- Giá trị
99,JSON.stringify[value] JSON.stringify[value, replacer] JSON.stringify[value, replacer, space]
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ànhJSON.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[{}]; // '{}' 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 khi chuyển các giá trị "thuần túy" nhưJSON.stringify[value] JSON.stringify[value, replacer] JSON.stringify[value, replacer, space]
95 hoặcJSON.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
96JSON.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
- Các số
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. ]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
- 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
01 [bao gồm] đượ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
- Đối với các đối tượng khác
- Tất cả các thuộc tính có khóa
7 sẽ bị bỏ qua hoàn toàn, ngay cả khi sử dụng tham sốJSON.stringify[{}]; // '{}' JSON.stringify[true]; // 'true' JSON.stringify["foo"]; // '"foo"' JSON.stringify[[1, "false", false]]; // '[1,"false",false]' JSON.stringify[[NaN, null, Infinity]]; // '[null,null,null]' JSON.stringify[{ x: 5 }]; // '{"x":5}' JSON.stringify[new Date[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
- Nếu giá trị có phương thức
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ứcJSON.stringify[value] JSON.stringify[value, replacer] JSON.stringify[value, replacer, space]
97 khi được gọi sẽ được đánh số thứ tự.JSON.stringify[value] JSON.stringify[value, replacer] JSON.stringify[value, replacer, space]
2 gọiJSON.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àmJSON.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
- 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
2 được gọi trực tiếp trên đối tượng này, một chuỗi rỗngJSON.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ứcJSON.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 trả về một chuỗi [giống nhưJSON.stringify[value] JSON.stringify[value, replacer] JSON.stringify[value, replacer, space]
94]. Do đó, chúng sẽ được xâu thành chuỗiJSON.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
- Chỉ vô số thuộc tính riêng được truy cập. Điều này có nghĩa là
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ànhJSON.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]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
- Tất cả các thuộc tính có khóa
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ảngLà 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
4Hà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
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. ]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
- Nếu bạn trả lại một
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ặcJSON.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, thuộc tính không được bao gồm trong đầu raJSON.stringify[value] JSON.stringify[value, replacer] JSON.stringify[value, replacer, space]
- 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
4 trên mỗi thuộc tínhJSON.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
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ượcThô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ácTham 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]
9JSON.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
9Thụ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
0Sử 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
9Xá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
6Vì đị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òngJSON.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ònTrong 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
2function 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}'
0Cá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
0Như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
1Thay đổ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