JSON.stringify(), JSON.parse()
๐ JSON.stringify()
โถ๏ธ JSON.stringify() ๋?
JavaScript ๊ฐ, ๊ฐ์ฒด โ JSON ๋ฌธ์์ด๋ก ๋ณํ
JSON์ ์ผ๋ฐ์ ์ธ ์ฉ๋๋ ์น ์๋ฒ์ ๋ฐ์ดํฐ๋ฅผ ๊ตํํ๋ ๊ฒ์ด๋ค. ์น ์๋ฒ์ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ผ ๋ ๋ฐ์ดํฐ๋ ๋ฌธ์์ด์ด์ด์ผ ํ๋ฏ๋ก, JSON.stringify()
๋ฅผ ์ด์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ๋ค.
JSON.stringify({ x: 5, y: 6 }); // '{"X":5,"y":6}'
JSON.stringify([3, "false", false]); // '[3,"false",false]'
JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)); // '"2006-01-02T06:04:05.000Z"'
โถ๏ธ ๊ท์น
- ๋ฐฐ์ด์ด ์๋ ๊ฐ์ฒด์ ์์ฑ๋ค์ ์ด๋ค ํน์ ํ ์์์ ๋ฐ๋ผ ๋ฌธ์์ดํ ๋ ๊ฒ์ด๋ผ๊ณ ๋ณด์ฅ๋์ง ์๋๋ค. ๊ฐ์ ๊ฐ์ฒด์ ๋ฌธ์์ดํ์ ์์ด์ ์์ฑ์ ์์์ ์์กดํ์ง ์๋๋ค.
Boolean
,Number
,String
๊ฐ์ฒด๋ค์ ๋ฌธ์์ดํ ๋ ๋ ์ ํต์ ์ธ ๋ณํ ์๋ฏธ์ ๋ฐ๋ผ ์ฐ๊ด๋ ๊ธฐ๋ณธํ(primitive) ๊ฐ์ผ๋ก ๋ณ๊ฒฝ๋๋ค.undefined
, ํจ์, ์ฌ๋ณผ(symbol)์ ๋ณํ๋ ๋ ์๋ต๋๊ฑฐ๋(๊ฐ์ฒด ์์ ์์ ๊ฒฝ์ฐ),null
๋ก ๋ณํ๋๋ค(๋ฐฐ์ด ์์ ์์ ๊ฒฝ์ฐ)- ์ฌ๋ณผ์ ํค๋ก ๊ฐ์ง๋ ์์ฑ๋ค์
replacer
ํจ์๋ฅผ ์ฌ์ฉํ๋๋ผ๋ ์์ ํ ๋ฌด์๋๋ค. - ์ด๊ฑฐ ๋ถ๊ฐ๋ฅํ ์์ฑ๋ค์ ๋ฌด์๋๋ค.
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify({ x: 5 }); // '{"x":5}'
JSON.stringify(new Date(2006, 0, 2, 15, 4, 5));
// '"2006-01-02T15:04:05.000Z"'
JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}' or '{"y":6,"x":5}'
JSON.stringify([new Number(1), new String("false"), new Boolean(false)]);
// '[1,"false",false]'
// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, function (k, v) {
if (typeof k === "symbol") {
return "a symbol";
}
});
// '{}'
// Non-enumerable properties:
JSON.stringify(
Object.create(null, {
x: { value: "x", enumerable: false },
y: { value: "y", enumerable: true },
})
);
// '{"y":"y"}'
โถ๏ธ ์ง๋ ฌํ (serialize)
const message = {
sender: "๊น์ฝ๋ฉ",
receiver: "๋ฐํด์ปค",
message: "ํด์ปค์ผ ์ค๋ ์ ๋
๊ฐ์ด ๋จน์๋?",
createdAt: "2021-01-12 10:10:10",
};
let transferableMessage = JSON.stringify(message);
console.log(transferableMessage);
// '{"sender":"๊น์ฝ๋ฉ","receiver":"๋ฐํด์ปค","message":"ํด์ปค์ผ ์ค๋ ์ ๋
๊ฐ์ด ๋จน์๋?","createdAt":"2021-01-12 10:10:10"}'
console.log(typeof transferableMessage);
// 'string'
stringify ํ๋ ์ด ๊ณผ์ ์ ์ง๋ ฌํ(serialize)ํ๋ค๊ณ ๋งํ๋ค.
JSON์ผ๋ก ๋ณํ๋ ๊ฐ์ฒด์ ํ์
์ ๋ฌธ์์ด์ด๋ค. ๋ฐ์ ์๋ ๊ฐ์ฒด๋ฅผ ์ง๋ ฌํ ํ ๋ฌธ์์ด๋ก ๋๊ตฐ๊ฐ์๊ฒ ๊ฐ์ฒด์ ๋ด์ฉ์ ๋ณด๋ผ ์ ์๋ค. ๊ทธ๋ ๋ค๋ฉด ์์ ์๋ ์ด ๋ฌธ์์ด ๋ฉ์์ง๋ฅผ ์ด๋ป๊ฒ ๋ค์ ๊ฐ์ฒด์ ํํ๋ก ๋ง๋ค ์ ์์๊น? JSON.stringify()
์ ์ ๋ฐ๋์ ์์
์ ์ํํ๋ JSON.parse()
๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
๐ JSON.parse()
JSON ๋ฌธ์์ด โ JavaScript ๊ฐ, ๊ฐ์ฒด๋ก ๋ณํ
JSON์ ์ผ๋ฐ์ ์ธ ์ฉ๋๋ ์น ์๋ฒ์ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ๊ฒ์ด๋ค. ์น ์๋ฒ์์ ๋ฐ์ดํฐ๋ฅผ ์์ ํ ๋ ๋ฐ์ดํฐ๋ ํญ์ ๋ฌธ์์ด์ด๋ค. ๊ฐ์ ธ์จ ๋ฐ์ดํฐ๋ฅผ JSON.parse()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด JSON ๋ฌธ์์ด์ ๊ตฌ๋ฌธ์ ๋ถ์ํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ์์ JavaScript ๊ฐ์ด๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
const json = '{"result":true,"count":42}';
const obj = JSON.parse(json);
console.log(obj); // Object { result: true, count: 42}
console.log(obj.count); // 42
console.log(obj.result); // true
JSON.parse("{}"); // {}
JSON.parse("true"); // true
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse("null"); // null
โ ํํ ์ผํ ์ฌ์ฉ ๋ถ๊ฐ
JSON.parse("[1, 2, 3, 4, ]"); //SyntaxError
JSON.parse('{"foo" : 1, }'); //SyntaxError
๋ฐ์ดํฐ ์ ์ฅ ์์
ex: localStorage์ ๋ฐ์ดํฐ ์ ์ฅ
// Storing data:
const myObj = { name: "John", age: 31, city: "New York" };
const myJSON = JSON.stringify(myObj);
localStorage.setItem("testJSON", myJSON);
// Retrieving data:
let text = localStorage.getItem("testJSON");
let obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.name;
Reference
MDN : JSON.stringify, JSON.parse()
[JSON] JSON ์ด๋ ๋ฌด์์ธ๊ฐ? ๊ฐ๋จ ์ ๋ฆฌ ๋ฐ ์์ ๋ฅผ ํตํ ์ฌ์ฉ ๋ฐฉ๋ฒ
Leave a comment