Mengonversi objek Java menjadi string json dalam javascript

Metode statis

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 mengonversi nilai JavaScript menjadi string JSON, secara opsional mengganti nilai jika fungsi pengganti ditentukan atau secara opsional menyertakan hanya properti yang ditentukan jika array pengganti ditentukan

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

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

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

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

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

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

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

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

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

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

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

Nilai yang akan dikonversi menjadi string 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 Opsional

Fungsi yang mengubah perilaku proses stringifikasi, atau larik string dan angka yang menentukan properti

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 untuk disertakan dalam output. Jika
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 adalah sebuah larik, semua elemen dalam larik ini yang bukan string atau angka (baik primitif atau objek pembungkus), termasuk nilai
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, akan diabaikan sepenuhnya. Jika
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 adalah apa pun selain fungsi atau larik (mis. 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 atau tidak disediakan), semua properti kunci-string dari objek disertakan dalam string JSON yang dihasilkan

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 Opsional

String atau angka yang digunakan untuk menyisipkan spasi putih (termasuk lekukan, karakter pemisah baris, dll. ) ke dalam string JSON keluaran untuk tujuan keterbacaan

Jika ini adalah angka, ini menunjukkan jumlah karakter spasi yang akan digunakan sebagai lekukan, dijepit ke 10 (yaitu, angka yang lebih besar dari

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 diperlakukan seolah-olah
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). Nilai kurang dari 1 menunjukkan bahwa tidak boleh ada spasi yang digunakan

Jika ini adalah string, string (atau 10 karakter pertama dari string, jika lebih panjang dari itu) dimasukkan sebelum setiap objek atau larik bersarang

Jika

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 adalah apa pun selain string atau angka (dapat berupa objek primitif atau pembungkus) — misalnya, apakah
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 atau tidak disediakan — tidak ada spasi putih yang digunakan

String JSON yang mewakili nilai yang diberikan, atau tidak ditentukan

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

Dilempar jika salah satu dari berikut ini benar

  • 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 berisi referensi melingkar
  • Nilai
    function replacer(key, value) {
      // Filtering out properties
      if (typeof value === "string") {
        return undefined;
      }
      return value;
    }
    
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    JSON.stringify(foo, replacer);
    // '{"week":45,"month":7}'
    
    _7 ditemukan

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 mengonversi nilai menjadi notasi JSON yang diwakili oleh nilai tersebut. Nilai-nilai yang stringified dengan cara berikut

  • function replacer(key, value) {
      // Filtering out properties
      if (typeof value === "string") {
        return undefined;
      }
      return value;
    }
    
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    JSON.stringify(foo, replacer);
    // '{"week":45,"month":7}'
    
    _9,
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    0,
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    1, dan
    function replacer(key, value) {
      // Filtering out properties
      if (typeof value === "string") {
        return undefined;
      }
      return value;
    }
    
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    JSON.stringify(foo, replacer);
    // '{"week":45,"month":7}'
    
    7 (dapat diperoleh melalui
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    3) objek dikonversi ke nilai primitif yang sesuai selama stringifikasi, sesuai dengan semantik konversi tradisional.
    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 objek (dapat diperoleh melalui
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    3) diperlakukan sebagai objek biasa
  • Mencoba membuat cerita bersambung
    function replacer(key, value) {
      // Filtering out properties
      if (typeof value === "string") {
        return undefined;
      }
      return value;
    }
    
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    JSON.stringify(foo, replacer);
    // '{"week":45,"month":7}'
    
    _7 nilai akan dibuang. Namun, jika BigInt memiliki metode
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    7 (melalui patching monyet.
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    _8), metode tersebut dapat memberikan hasil serialisasi. Kendala ini memastikan bahwa perilaku serialisasi yang tepat (dan, kemungkinan besar, deserialisasi yang menyertainya) selalu disediakan secara eksplisit oleh pengguna
  • Nilai ________19______9,
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    0, dan
    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 bukan nilai JSON yang valid. Jika ada nilai seperti itu yang ditemui selama konversi, nilai tersebut dihilangkan (bila ditemukan dalam objek) atau diubah menjadi
    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 (bila ditemukan dalam larik).
    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 dapat mengembalikan
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    9 saat meneruskan nilai "murni" seperti
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    5 atau
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    6
  • Angka
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    7 dan
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    8, serta nilai
    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, semuanya dianggap
    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. (Tetapi tidak seperti nilai pada poin sebelumnya, nilai tersebut tidak akan pernah dihilangkan. )
  • Array diserialkan sebagai array (ditutup dengan tanda kurung siku). Hanya indeks larik antara 0 dan
    console.log(JSON.stringify({ a: 2 }, null, " "));
    /*
    {
     "a": 2
    }
    */
    
    1 (inklusif) yang diserialisasi;
  • Untuk objek lainnya
    • Semua properti dengan kunci
      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 akan diabaikan sepenuhnya, bahkan saat menggunakan parameter
    • Jika nilainya memiliki metode
      function makeReplacer() {
        let isInitial = true;
      
        return (key, value) => {
          if (isInitial) {
            isInitial = false;
            return value;
          }
          if (key === "") {
            // Omit all properties with name "" (except the initial object)
            return undefined;
          }
          return value;
        };
      }
      
      const replacer = makeReplacer();
      console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
      
      7, itu bertanggung jawab untuk menentukan data apa yang akan diserialkan. Alih-alih objek yang diserialisasi, nilai yang dikembalikan oleh metode
      function makeReplacer() {
        let isInitial = true;
      
        return (key, value) => {
          if (isInitial) {
            isInitial = false;
            return value;
          }
          if (key === "") {
            // Omit all properties with name "" (except the initial object)
            return undefined;
          }
          return value;
        };
      }
      
      const replacer = makeReplacer();
      console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
      
      7 saat dipanggil akan diserialisasi.
      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 memanggil
      console.log(JSON.stringify({ a: 2 }, null, " "));
      /*
      {
       "a": 2
      }
      */
      
      7 dengan satu parameter,
      console.log(JSON.stringify({ a: 2 }, null, " "));
      /*
      {
       "a": 2
      }
      */
      
      8, yang memiliki semantik yang sama dengan parameter
      console.log(JSON.stringify({ a: 2 }, null, " "));
      /*
      {
       "a": 2
      }
      */
      
      8 dari fungsi
      • jika objek ini adalah nilai properti, nama properti
      • jika dalam array, indeks dalam array, sebagai string
      • jika
        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 langsung dipanggil pada objek ini, string kosong
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      _2 objek mengimplementasikan metode
      function makeReplacer() {
        let isInitial = true;
      
        return (key, value) => {
          if (isInitial) {
            isInitial = false;
            return value;
          }
          if (key === "") {
            // Omit all properties with name "" (except the initial object)
            return undefined;
          }
          return value;
        };
      }
      
      const replacer = makeReplacer();
      console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
      
      7 yang mengembalikan string (sama seperti
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      4). Dengan demikian, mereka akan dirangkai sebagai string
    • Hanya properti milik sendiri yang dapat dihitung yang dikunjungi. Ini berarti
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      5,
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      6, dll. akan menjadi
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      _7. Anda dapat menggunakan parameter untuk membuat cerita bersambung menjadi sesuatu yang lebih bermanfaat. Properti dikunjungi menggunakan algoritme yang sama dengan
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      9, yang memiliki urutan yang terdefinisi dengan baik dan stabil di seluruh implementasi. Misalnya,
      const obj = {
        data: "data",
      
        toJSON(key) {
          return key ? `Now I am a nested object under key '${key}'` : this;
        },
      };
      
      JSON.stringify(obj);
      // '{"data":"data"}'
      
      JSON.stringify({ obj });
      // '{"obj":"Now I am a nested object under key 'obj'"}'
      
      JSON.stringify([obj]);
      // '["Now I am a nested object under key '0'"]'
      
      _0 pada objek yang sama akan selalu menghasilkan string yang sama, dan
      const obj = {
        data: "data",
      
        toJSON(key) {
          return key ? `Now I am a nested object under key '${key}'` : this;
        },
      };
      
      JSON.stringify(obj);
      // '{"data":"data"}'
      
      JSON.stringify({ obj });
      // '{"obj":"Now I am a nested object under key 'obj'"}'
      
      JSON.stringify([obj]);
      // '["Now I am a nested object under key '0'"]'
      
      1 akan menghasilkan objek dengan urutan kunci yang sama seperti aslinya (dengan asumsi objek tersebut benar-benar dapat diserialkan JSON)

Parameter

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 dapat berupa fungsi atau larik

Sebagai larik, elemennya menunjukkan nama properti di objek yang harus disertakan dalam string JSON yang dihasilkan. Hanya nilai string dan angka yang diperhitungkan;

Sebagai fungsi, dibutuhkan dua parameter.

console.log(JSON.stringify({ a: 2 }, null, " "));
/*
{
 "a": 2
}
*/
_8 dan
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 sedang dirangkai. Objek tempat kunci ditemukan disediakan sebagai konteks
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
const obj = {
  data: "data",

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

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

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

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

Fungsi

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 dipanggil untuk objek awal yang dirangkai juga, dalam hal ini
console.log(JSON.stringify({ a: 2 }, null, " "));
/*
{
 "a": 2
}
*/
8 adalah string kosong (
const obj = {
  data: "data",

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

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

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

JSON.stringify([obj]);
// '["Now I am a nested object under key '0'"]'
9). Ini kemudian dipanggil untuk setiap properti pada objek atau larik yang sedang dirangkai. Indeks array akan diberikan dalam bentuk string sebagai
console.log(JSON.stringify({ a: 2 }, null, " "));
/*
{
 "a": 2
}
*/
8. Nilai properti saat ini akan diganti dengan nilai pengembalian
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 untuk stringifikasi. Ini berarti

  • Jika Anda mengembalikan angka, string, boolean, atau
    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, nilai tersebut langsung diserialisasikan dan digunakan sebagai nilai properti. (Mengembalikan BigInt juga akan melempar. )
  • Jika Anda mengembalikan
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    _0,
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(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, atau
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    9, properti tidak disertakan dalam output
  • Jika Anda mengembalikan objek lain, objek tersebut dirangkai secara rekursif, memanggil fungsi
    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 pada setiap properti

Catatan. Saat mem-parsing JSON yang dihasilkan dengan fungsi

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, Anda mungkin ingin menggunakan parameter untuk melakukan operasi sebaliknya

Biasanya, indeks elemen array tidak akan pernah bergeser (bahkan ketika elemen adalah nilai yang tidak valid seperti fungsi, itu akan menjadi

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 alih-alih dihilangkan). Menggunakan fungsi
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 memungkinkan Anda untuk mengontrol urutan elemen array dengan mengembalikan array yang berbeda

Parameter

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 dapat digunakan untuk mengontrol spasi dalam string terakhir

  • Jika berupa angka, level berurutan dalam stringifikasi masing-masing akan diindentasi oleh banyak karakter spasi ini
  • Jika berupa string, level yang berurutan akan diindentasi oleh string ini

Setiap tingkat lekukan tidak akan pernah lebih dari 10. Nilai angka

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 dijepit menjadi 10, dan nilai string dipotong menjadi 10 karakter

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}'
_

Jika Anda ingin

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 untuk membedakan objek awal dari kunci dengan properti string kosong (karena keduanya akan memberikan string kosong sebagai kunci dan berpotensi objek sebagai nilai), Anda harus melacak jumlah iterasi (jika

function makeReplacer() {
  let isInitial = true;

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

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

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

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

Indentasi keluaran dengan satu spasi

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

Menggunakan karakter tab meniru tampilan cetak cantik standar

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

Mendefinisikan

function makeReplacer() {
  let isInitial = true;

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

const replacer = makeReplacer();
console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
7 untuk sebuah objek memungkinkan mengesampingkan perilaku serialisasinya

const obj = {
  data: "data",

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

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

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

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

Karena format JSON tidak mendukung referensi objek (walaupun ada draf IETF),

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 akan dilemparkan jika seseorang mencoba menyandikan objek dengan referensi melingkar

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

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

Untuk membuat serialisasi referensi melingkar, Anda dapat menggunakan pustaka yang mendukungnya (mis. g. siklus. js oleh Douglas Crockford) atau implementasikan sendiri solusinya, yang akan membutuhkan pencarian dan penggantian (atau penghapusan) referensi siklik dengan nilai yang dapat diserialisasi

Jika Anda menggunakan

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 untuk menyalin objek secara mendalam, Anda mungkin ingin menggunakan
// Creating an example of JSON
const session = {
  screens: [],
  state: true,
};
session.screens.push({ name: "screenA", width: 450, height: 250 });
session.screens.push({ name: "screenB", width: 650, height: 350 });
session.screens.push({ name: "screenC", width: 750, height: 120 });
session.screens.push({ name: "screenD", width: 250, height: 60 });
session.screens.push({ name: "screenE", width: 390, height: 120 });
session.screens.push({ name: "screenF", width: 1240, height: 650 });

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

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

// Now restoredSession variable contains the object that was saved
// in localStorage
console.log(restoredSession);
7, yang mendukung referensi melingkar. API mesin JavaScript untuk serialisasi biner, seperti , juga mendukung referensi melingkar

Dalam kasus di mana Anda ingin menyimpan objek yang dibuat oleh pengguna Anda dan mengizinkannya dipulihkan bahkan setelah browser ditutup, contoh berikut adalah model untuk penerapan

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

// Creating an example of JSON
const session = {
  screens: [],
  state: true,
};
session.screens.push({ name: "screenA", width: 450, height: 250 });
session.screens.push({ name: "screenB", width: 650, height: 350 });
session.screens.push({ name: "screenC", width: 750, height: 120 });
session.screens.push({ name: "screenD", width: 250, height: 60 });
session.screens.push({ name: "screenE", width: 390, height: 120 });
session.screens.push({ name: "screenF", width: 1240, height: 650 });

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

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

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

Mesin yang mengimplementasikan JSON yang dibuat dengan baik. spesifikasi stringify akan merangkai pengganti tunggal (titik kode apa pun dari U+D800 ke U+DFFF) menggunakan urutan pelolosan Unicode daripada secara harfiah (menghasilkan pengganti tunggal). Sebelum perubahan ini, string tersebut tidak dapat dikodekan dalam UTF-8 atau UTF-16 yang valid

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

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

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

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

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

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

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

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

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

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

Tetapi dengan perubahan ini

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 mewakili satu-satunya pengganti menggunakan urutan pelarian JSON yang dapat dikodekan dalam UTF-8 atau UTF-16 yang valid

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

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

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

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

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

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

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

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

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

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

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

Perubahan ini harus kompatibel mundur selama Anda meneruskan hasil

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 ke API seperti
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 yang akan menerima teks JSON yang valid, karena mereka akan memperlakukan Unicode escapes dari lone surrogate identik dengan lone surrogate itu sendiri. Hanya jika Anda secara langsung menginterpretasikan hasil
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 Anda perlu hati-hati menangani dua kemungkinan penyandian
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 dari poin kode ini

Bagaimana cara mengubah objek Java ke JSON di JavaScript?

Buat kelas Java untuk mengonversi objek Java ke JSON. .
Langkah 1. Buat proyek Maven. Pada langkah pertama, kita perlu membuat project maven menggunakan Eclipse IDE. .
Langkah 2. Tambahkan ketergantungan GSON di pom. xml. .
Langkah 3. Buat POJO untuk diubah menjadi JSON. .
Langkah 4. Buat kelas Java untuk mengonversi objek Java menjadi JSON

Bagaimana cara mengonversi objek objek ke JSON di JavaScript?

Untuk mengonversi objek menjadi string JSON di JavaScript, Anda dapat menggunakan metode JSON. stringify(nilai, pengganti, spasi) metode. JSON. stringify() metode membuat serial objek, array, atau nilai primitif menjadi string data JSON

Bagaimana cara mem-parsing objek ke JSON di JavaScript?

Gunakan fungsi JavaScript JSON. parse() untuk mengonversi teks menjadi objek JavaScript . const obj = JSON. parse('{"nama". "Yohanes", "umur". 30, "kota". "New York"}'); .

Bagaimana cara mengubah objek JSON menjadi string JSON di Java?

Objek Java ke Json String. Tutorial .
Langkah 1. Sertakan file JACKSON JAR ke dalam classpath Anda. .
Langkah 2. Gunakan kelas Jackson API ObjectMapper untuk mengonversi Java Object menjadi string JSON. .
Langkah 3. RUN menggunakan JACKSONapitoConvertJavaOBJtoJSONstring. .
Langkah 1. Sertakan file JAR GSON ke dalam classpath Anda