JavaScript Object Notationμ μ½μλ‘ μλ°μ€ν¬λ¦½νΈ κ°μ²΄ λ¬Έλ².
λ°μ΄ν°λ₯Ό μ μ₯νκ±°λ μ μ‘ν λ λ§μ΄ μ¬μ©λλ ꡬ쑰νλ λ°μ΄ν° ννμ μν νμ€ νμμ΄λ€. νλ‘κ·Έλλ° λ¬Έλ²μ΄ μλ λ¨μν λ°μ΄ν°λ₯Ό νμνλ νν λ°©λ²μΌλ‘, μλ²μ ν΄λΌμ΄μΈνΈ κ°μ κ΅λ₯μμ λ§μ΄ μ¬μ©λλ€.
μλ°μ€ν¬λ¦½νΈμ μ’ μλμ§ μλ μΈμ΄ λ 립ν λ°μ΄ν° ν¬λ§·μΌλ‘, λλΆλΆμ νλ‘κ·Έλλ° μΈμ΄μμ μ¬μ©ν μ μλ€.
JSONμ μλ°μ€ν¬λ¦½νΈ κ°μ²΄μ μ μ¬νκ² ν€μ κ°μΌλ‘ ꡬμ±λ μμν ν μ€νΈλ€. JSONμ ν€λ λ°λμ ν°λ°μ΄ν(μμλ°μ΄ν μ¬μ© λΆκ°)λ‘ λ¬Άμ΄μΌ νλ€. κ°μ κ°μ²΄ 리ν°λ΄κ³Ό κ°μ νκΈ°λ²μ κ·Έλλ‘ μ¬μ©ν μ μμ§λ§, λ¬Έμμ΄μ λ°λμ ν°λ°μ΄νλ‘ λ¬Άμ΄μΌ νλ€.
{ "name": "νκΈΈλ" }
λ€μν λ°μ΄ν° νμμ μ¬μ©ν μ μλ€.
(String, Number, Object, Array, Null, Boolean)
{
"name": "νκΈΈμ",
"age": 30,
"gender": "λ¨",
"interests": ["μμ
", "λμ€"],
"family": {"fatehr": "νν¬κΈΈ", "mom": "κΉμ°μΌ"},
"school": null,
"graduate": true
}
JSON λ΄μ₯ λ©μλλ₯Ό ν΅ν΄ JSON κ°μ²΄λ₯Ό μλ°μ€ν¬λ¦½νΈ κ°μ²΄λ‘ λ³νν μλ, μλ°μ€ν¬λ¦½νΈ κ°μ²΄λ₯Ό JSON κ°μ²΄λ‘ λ³νν μλ μλ€.
JSON κ°μ²΄ -> JavaScript κ°μ²΄
Syntax
JSON.parse(text)
JSON.parse(text, reviver)
Example
let jsonData = `{
"name": "νκΈΈμ",
"age": 30,
"gender": "λ¨",
"interests": ["μμ
", "λμ€"],
"family": {"fatehr": "νν¬κΈΈ", "mom": "κΉμ°μΌ"},
"school": null,
"graduate": true
}`;
let jsData = JSON.parse(jsonData);
console.log(jsData);
/*
{
name: 'νκΈΈμ',
age: 30,
gender: 'λ¨',
interests: [ 'μμ
', 'λμ€' ],
family: { fatehr: 'νν¬κΈΈ', mom: 'κΉμ°μΌ' },
school: null,
graduate: true
}
*/
console.log(typeof jsData); // object
μλ°μ€ν¬λ¦½νΈ κ°μ²΄ β JSON κ°μ²΄
Syntax
JSON.stringify(value)
JSON.stringify(value, replacer)
JSON.stringify(value, replacer, space)
Example
let jsData = {
"name": "νκΈΈμ",
"age": 30,
"gender": "λ¨",
"interests": ["μμ
", "λμ€"],
"family": {"fatehr": "νν¬κΈΈ", "mom": "κΉμ°μΌ"},
"school": null,
"graduate": true
};
let jsonData = JSON.stringify(jsData);
console.log(jsonData);
// {"name":"νκΈΈμ","age":30,"gender":"λ¨","interests":["μμ
","λμ€"],"family":{"fatehr":"νν¬κΈΈ","mom":"κΉμ°μΌ"},"school":null,"graduate":true}
console.log(typeof jsonData); // string
λ³νν μλ°μ€ν¬λ¦½νΈ κ°μ²΄κ° λ§μ μμ λ°μ΄ν°λ₯Ό ν¬ν¨νκ³ μλ€λ©΄ κ°λ μ±μ΄ νμ νκ² λ¨μ΄μ§ κ²μ΄λ€. μ΄λλ stringify() λ©μλμ μΈ λ²μ§Έ μΈμμ λ€μ¬μ°κΈ°ν 곡백μ ν¬κΈ°λ₯Ό μ λ¬νμ¬ μ§μ ν μ μλ€.
// λ μΉΈ λ€μ¬μ°κΈ°
jsonData = JSON.stringify(jsData, null, 2);
console.log(jsonData);
/*
{
"name": "νκΈΈμ",
"age": 30,
"gender": "λ¨",
"interests": [
"μμ
",
"λμ€"
],
"family": {
"fatehr": "νν¬κΈΈ",
"mom": "κΉμ°μΌ"
},
"school": null,
"graduate": true
}
*/
MDN JSON JSON.parse() JSON.stringify()
μ΄μ
λͺ¨, γλͺ¨λ μλ°μ€ν¬λ¦½νΈ Deep Diveγ, μν€λΆμ€