Baseline Widely available
JSON.stringify()
ë©ìëë JavaScript ê°ì´ë ê°ì²´ë¥¼ JSON 문ìì´ë¡ ë³íí©ëë¤. ì íì ì¼ë¡, replacer
를 í¨ìë¡ ì ë¬í ê²½ì° ë³í ì ê°ì ë³íí ì ìê³ , ë°°ì´ë¡ ì ë¬í ê²½ì° ì§ì í ìì±ë§ ê²°ê³¼ì í¬í¨í©ëë¤.
console.log(JSON.stringify({ x: 5, y: 6 }));
// Expected output: '{"x":5,"y":6}'
console.log(
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]),
);
// Expected output: '[3,"false",false]'
console.log(JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] }));
// Expected output: '{"x":[10,null,null,null]}'
console.log(JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)));
// Expected output: '"2006-01-02T15:04:05.000Z"'
구문
JSON.stringify(value[, replacer[, space]])
매ê°ë³ì
value
JSON 문ìì´ë¡ ë³íí ê°.
replacer
Optional
문ìì´í ëì ë°©ìì ë³ê²½íë í¨ì, í¹ì JSON 문ìì´ì í¬í¨ë ê° ê°ì²´ì ìì±ë¤ì ì íí기 ìí íì´í¸ë¦¬ì¤í¸(whitelist)ë¡ ì°ì´ë String
ê³¼ Number
ê°ì²´ë¤ì ë°°ì´. ì´ ê°ì´ null ì´ê±°ë ì ê³µëì§ ìì¼ë©´, ê°ì²´ì 모ë ìì±ë¤ì´ JSON 문ìì´ ê²°ê³¼ì í¬í¨ëë¤.
space
Optional
ê°ë
ì±ì 목ì ì¼ë¡ JSON 문ìì´ ì¶ë ¥ì 공백ì ì½ì
íëë° ì¬ì©ëë String
ëë Number
ê°ì²´. ì´ê²ì´ Number
ë¼ë©´, 공백ì¼ë¡ ì¬ì©ëë ì¤íì´ì¤(space)ì ì를 ëíë¸ë¤; ì´ ìê° 10 ë³´ë¤ í¬ë©´ 10 ì¼ë¡ ì íëë¤. 1 ë³´ë¤ ìì ê°ì ì¤íì´ì¤ê° ì¬ì©ëì§ ìë ê²ì ëíë¸ë¤. ì´ê²ì´ String
ì´ë¼ë©´, ê·¸ 문ìì´(ë§ì½ 길ì´ê° 10 ë³´ë¤ ê¸¸ë¤ë©´, 첫ë²ì§¸ 10 ê°ì 문ì)ì´ ê³µë°±ì¼ë¡ ì¬ì©ëë¤. ì´ ë§¤ê° ë³ìê° ì ê³µëì§ ìëë¤ë©´(ëë null ì´ë©´), ê³µë°±ì´ ì¬ì©ëì§ ìëë¤.
주ì´ì§ ê°ê³¼ ëìíë JSON 문ìì´.
ìì¸ìí 참조를 ë°ê²¬í ê²½ì° TypeError
(cyclic object value
).
JSON.stringify()
ë ê°ì JSON í기ë²ì¼ë¡ ë³ííë¤.
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"}'
replacer
ë§¤ê° ë³ì
replacer
매ê°ë³ìë í¨ì ëë ë°°ì´ì´ ë ì ìë¤. í¨ìì¼ ëë 문ìì´í ë key ì value, 2ê°ì 매ê°ë³ì를 ë°ëë¤. key ê° ë°ê²¬ë ê°ì²´ë 리íë ì´ìì this
매ê°ë³ìë¡ ì ê³µëë¤. 맨 ì²ììë 문ìì´íë ê·¸ ê°ì²´ë¥¼ ëíë´ë ë¹ì´ ìë keyì í¨ê» í¸ì¶ëê³ , ê·¸ ë¤ììë 문ìì´íë ê·¸ ê°ì²´ë ë°°ì´ì ê° ìì±ì ëí´ í¸ì¶ëë¤. ì´ê²ì JSON 문ìì´ì ì¶ê°ëì´ì¼ íë ê°ì ë°íí´ì¼íë¤:
Number
를 ë°ííë©´, JSON 문ìì´ì ì¶ê°ë ë ê·¸ ì를 ëíë´ë 문ìì´ì´ ê·¸ ìì±ì ê°ì¼ë¡ ì¬ì©ëë¤.String
ì ë°ííë©´, ê·¸ê²ì´ JSON 문ìì´ì ì¶ê°ë ë ìì±ì ê°ì¼ë¡ ì¬ì©ëë¤.Boolean
ì ë°ííë©´, ê·¸ê²ì´ JSON 문ìì´ì ì¶ê°ë ë "true" ëë "false" ì´ ìì±ì ê°ì¼ë¡ ì¬ì©ëë¤.replacer
í¨ì를 ê°ê°ì ìì±ì ëí´ í¸ì¶íë©° ìíì ì¼ë¡ JSON 문ìì´ë¡ 문ìì´íëë¤. ê·¸ ê°ì²´ê° í¨ìì¸ ê²½ì°ìë JSON 문ìì´ì ì무ê²ë ì¶ê°ëì§ ìëë¤.undefined
를 ë°ííë©´, ê·¸ ìì±ì JSON 문ìì´ ê²°ê³¼ì í¬í¨ëì§ ìëë¤.í¨ìì ëí ìì ì°¸ê³ : ì ì:
replacer
í¨ì를 ë°°ì´ë¡ë¶í° ê°ì ì ê±°í기ìí´ ì¬ì©í ì ìë¤. ë§ì½undefined
ë í¨ì를 ë°ííë¤ë©´null
ì´ ëì ì¬ì©ë ê²ì´ë¤.
function replacer(key, value) {
if (typeof value === "string") {
return undefined;
}
return value;
}
var foo = {
foundation: "Mozilla",
model: "box",
week: 45,
transport: "car",
month: 7,
};
var jsonString = JSON.stringify(foo, replacer);
JSON 문ìì´ ê²°ê³¼ë {"week":45,"month":7}
ì´ë¤.
replacer
ê° ë°°ì´ì¸ ê²½ì°, ê·¸ ë°°ì´ì ê°ì JSON 문ìì´ì ê²°ê³¼ì í¬í¨ëë ìì±ì ì´ë¦ì ëíë¸ë¤.
JSON.stringify(foo, ["week", "month"]);
// '{"week":45,"month":7}', ë¨ì§ "week" ì "month" ìì±ì í¬í¨íë¤
space
ë§¤ê° ë³ì
space
매ê°ë³ìë ìµì¢
문ìì´ì ê°ê²©ì ì ì´íë¤. ì«ìì¼ ê²½ì° ìµë 10ì ë§í¼ì 공백 문ì í¬ê¸°ë¡ ë¤ì¬ì°ê¸°ëë©°, 문ìì´ì¸ ê²½ì° í´ë¹ 문ìì´ ëë ì²ì 10ì ë§í¼ ë¤ì¬ì°ê¸° ëë¤.
JSON.stringify({ a: 2 }, null, " ");
// '{
// "a": 2
// }'
'\t'를 ì¬ì©íë©´ ì¼ë°ì ì¼ë¡ ë¤ì¬ì°ê¸° ë ì½ëì¤íì¼ê³¼ ì ì¬í¨.
JSON.stringify({ uno: 1, dos: 2 }, null, "\t");
// returns the string:
// '{
// "uno": 1,
// "dos": 2
// }'
toJSON()
ìë
If an object being stringified has a property named toJSON
whose value is a function, then the toJSON()
method customizes JSON stringification behavior: instead of the object being serialized, the value returned by the toJSON()
method when called will be serialized. For example:
var obj = {
foo: "foo",
toJSON: function () {
return "bar";
},
};
JSON.stringify(obj); // '"bar"'
JSON.stringify({ x: obj }); // '{"x":"bar"}'
Example of using JSON.stringify()
with localStorage
In a case where you want to store an object created by your user and allowing it to be restored even after the browser has been closed, the following example is a model for the applicability of JSON.stringify()
:
ê²½ê³ : Functions are not a valid JSON data type so they will not work. However, they can be displayed if first converted to a string (e.g. in the replacer), via the function's toString method. Also, some objects like Date
will be a string after JSON.parse()
.
// Creating an example of JSON
var 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
var restoredSession = JSON.parse(localStorage.getItem("session"));
// Now restoredSession variable contains the object that was saved
// in localStorage
console.log(restoredSession);
ëª
ì¸ì ë¸ë¼ì°ì í¸íì± ê°ì´ 보기
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4