๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daily Front_Minhhk

JSON.stringify() // JSON.parse() // Tree UI ๋ณธ๋ฌธ

Code๊ฐœ๋ฐœ์ผ์ง€

JSON.stringify() // JSON.parse() // Tree UI

Minhhk 2022. 12. 16. 22:36
๐Ÿ’ก JSON์€ JavaScript Object Notation์˜ ์ค„์ž„๋ง๋กœ, ๋ฐ์ดํ„ฐ ๊ตํ™˜์„ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด ํ˜•ํƒœ์˜ ํฌ๋งท

 

JSON.stringify

JSON.stringify : ๊ฐ์ฒด๋ฅผ JSON์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

JSON์œผ๋กœ ๋ณ€ํ™˜๋œ ๊ฐ์ฒด์˜ ํƒ€์ž…์€ ๋ฌธ์ž์—ด์ด๋‹ค.

JSON.stringifyํ•˜๋Š” ์ด ๊ณผ์ •์„ **์ง๋ ฌํ™”(serialize)**ํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

JSON.parse

JSON.parse : JSON์„ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

JSON.parse๋ฅผ ์ ์šฉํ•˜๋Š” ์ด ๊ณผ์ •์„ **์—ญ์ง๋ ฌํ™”(deserialize)**ํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

 

JSON์˜ ๊ธฐ๋ณธ ๊ทœ์น™

JSON์€ ์–ผํ• ๋ณด๊ธฐ์— ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด์™€ ๋ณ„๋ฐ˜ ๋‹ค๋ฅผ ๋ฐ”๊ฐ€ ์—†์–ด ๋ณด์ด์ง€๋งŒ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด์™€๋Š” ๋ฏธ๋ฌ˜ํ•˜๊ฒŒ ๋‹ค๋ฅธ ๊ทœ์น™์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

                                                                ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด                                                                    JSON

ํ‚ค ํ‚ค๋Š” ๋”ฐ์˜ดํ‘œ ์—†์ด ์“ธ ์ˆ˜ ์žˆ์Œ{ key : "property" } ๋ฐ˜๋“œ์‹œ ์Œ๋”ฐ์˜ดํ‘œ๋ฅผ ๋ถ™์—ฌ์•ผ ํ•จ'{"key":"property"}'
๋ฌธ์ž์—ด ๊ฐ’ ์ž‘์€๋”ฐ์˜ดํ‘œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ{ "key" : 'property' } ๋ฐ˜๋“œ์‹œ ํฐ๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ์•ผ ํ•จ'{"key":"property"}'
ํ‚ค์™€ ๊ฐ’ ์‚ฌ์ด ๊ณต๋ฐฑ ์‚ฌ์šฉ ๊ฐ€๋Šฅ{"key" : 'property'} ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ'{"key":"property"}'
ํ‚ค-๊ฐ’ ์Œ ์‚ฌ์ด ๊ณต๋ฐฑ ์‚ฌ์šฉ ๊ฐ€๋Šฅ{ "key":'property', num:1 } ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ'{"key":"property","num":1}'

 

 


 

๊ณผ์ œ1 - JSON.stringify

 

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ 3๊ฐ€์ง€๋กœ ๋‚˜๋ˆ ๋ณผ ์ˆ˜ ์žˆ๋‹ค

  1. ๊ฐ์ฒด๋‚˜ ๋ฐฐ์—ด์ด ์•„๋‹Œ ๊ฒฝ์šฐ1) number, null, boolean ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ด, string ์ผ๋•Œ๋Š” ์•ž๋’ค๋กœ "" ์ถ”๊ฐ€๋กœ ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค
  2. ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐArray.isArray๋กœ ๋ฐฐ์—ด์ธ์ง€ ํ™•์ธํ•œ ๋’ค stringifyJSON(์žฌ๊ท€ํ•จ์ˆ˜)์„ ์‚ฌ์šฉํ•ด์„œ ์ƒˆ ๋ฐฐ์—ด์— ๋„ฃ์–ด์ค€ ๋’ค ๋ฆฌํ„ด
  3. ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ ๊ฐ์ฒด์˜ key๊ฐ€ undefined || value ์— function, undefined ๊ฐ€ ๋“ค์–ด์™”์„ ๊ฒฝ์šฐ {}๋ฆฌํ„ด,,
  4. ๋‚˜๋จธ์ง€ ๊ฒฝ์šฐ์— ์†์„ฑ : ๊ฐ’ ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด๋กœ ์ €์žฅํ•œ ๋’ค ๋ฆฌํ„ด
/**
 * 1. Browser์— ์กด์žฌํ•˜๋Š” JSON.stringfy ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•ด ๋ด…๋‹ˆ๋‹ค.
 * JSON.stringfy ํ•จ์ˆ˜๋Š” input ๊ฐ’์„ JSON ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
 * ๋‹จ, undefined์™€ function์€ JSON์œผ๋กœ ์ƒ๋žต๋˜๊ฑฐ๋‚˜ null๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค.
 *
 * 2. stringfyJSON์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.
 * - Boolean์ด input์œผ๋กœ ์ฃผ์–ด์กŒ์„ ๊ฒฝ์šฐ
 * stringifyJSON(true);                // 'true'
 * - String์ด input์œผ๋กœ ์ฃผ์–ด์กŒ์„ ๊ฒฝ์šฐ
 * stringifyJSON('foo');               // '"foo"'
 * - Array๊ฐ€ input์œผ๋กœ ์ฃผ์–ด์กŒ์„ ๊ฒฝ์šฐ
 * stringifyJSON([1, 'false', false]); // '[1,"false",false]'
 * - Object๊ฐ€ input์œผ๋กœ ์ฃผ์–ด์กŒ์„ ๊ฒฝ์šฐ
 * stringifyJSON({ x: 5 });            // '{"x":5}'
 * - undefined, function์ด ์ฃผ์–ด์กŒ์„ ๊ฒฝ์šฐ
 * stringifyJSON(undefined)            // undefined
 * stringifyJSON(function(){})         // undefined
 * stringifyJSON({ x: undefined, y: function(){} })   // '{}'
 *
 * 3. spec/stringifyJSONSpec.js์˜ stringifiableObjects ๋ฐฐ์—ด์„ ์ฐธ๊ณ ํ•ด์„œ ํ…Œ์ŠคํŠธ์—์„œ ์–ด๋–ค input ๊ฐ’๋“ค์ด
 * ์ฃผ์–ด์ง€๊ณ , ์–ด๋–ป๊ฒŒ stringifyํ•ด ์ฃผ์–ด์•ผ ํ• ์ง€ ์ƒ๊ฐํ•ด ๋ณด์„ธ์š”.
 *
 * 4. ๊ทธ๋ƒฅ ํ…Œ์ŠคํŠธ ํ†ต๊ณผ๋ฅผ ํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•˜๋ฉด ๋  ๊ฑฐ์˜ˆ์š”.
 *  const stringifyJSON = JSON.stringify;
 *
 * ํ•˜์ง€๋งŒ ์ด ๊ณผ์ œ์˜ ๋ชฉ์ ์€ ์žฌ๊ท€๋ฅผ ๊ณต๋ถ€ํ•˜๋Š” ๊ฒƒ์ด๋‹ˆ, ์ฒ˜์Œ๋ถ€ํ„ฐ ๊ตฌํ˜„ํ•ด๋ด์•ผ๊ฒ ์ง€์š”?:
 */

 

 

 

์ฃผ์„๊ณผ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑ!!

 

function stringifyJSON(obj) {
  // boolean, number, null ์ผ๋•Œ
  if (typeof obj === "boolean" || typeof obj === "number" || obj === null) {
    return String(obj);
  }
  // string ์ผ ๋•Œ
  if (typeof obj === "string") {
    return `"${obj}"`;
  } 
  //ํ•จ์ˆ˜์™€ undefined๋Š” stringify๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  if (typeof obj === "undefined" || typeof obj === "function") {
    return "undefined";
  }
  // ๋ฐฐ์—ด
  if(Array.isArray(obj)) {
    let result = []
    for(let item of obj) {
      result.push(stringifyJSON(item))
    }
    return `[${result}]`
  }
  // ๊ฐ์ฒด
  if(typeof obj === 'object' && obj !== null){
    let str = ''
    for (let i in obj) {
      if(typeof obj[i] === 'function' || obj[i] === undefined) {
        return "{}"
      }
      let newkey = stringifyJSON(i)
      let newValue = stringifyJSON(obj[i])
      str = str + newkey + ":" + newValue + ','
    }
    str = str.slice(0,-1)
    return `{${str}}`
  }
}

 

 


 

๊ณผ์ œ2 - Tree UI

์ž์‹ ๋…ธ๋“œ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ๋ฐ˜๋ณต๋˜๋Š” <ul> ํ˜ธ์ถœํ•ด์•ผํ•œ๋‹ค. ๋งŒ์•ฝ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ <li>์— ์ด๋ฆ„์„ ๋„ฃ์œผ๋ฉด ๋œ๋‹ค.

 

result.html

์˜ˆ์‹œ์ธ tree ui ์ด๋‹ค,,

์ฃผ์–ด์ง„ menu ๋ฐฐ์—ด์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐ˜๋ณต๋ฌธ๊ณผ ์žฌ๊ท€๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ž‘์„ฑ!!!

 

fix_me.js

menu์˜ type์ด "group" ์ด๋ฉด, children์ด ์žˆ์œผ๋ฉฐ, children์˜ type์ด "item"์ด๋ฉด ๋๋‚œ๋‹ค.

์ด๊ฒƒ์œผ๋กœ menu ๋ฐฐ์—ด์„ ๋ฐ˜๋ณต๋Œ๋ฉฐ,

์กฐ๊ฑด์„ type === "group" ์ผ ๋•Œ์™€ ์•„๋‹ ๋•Œ ๋กœ ๋‚˜๋ˆ ์„œ

ํ•„์š”ํ•œ element๋ฅผ ์ƒ์„ฑํ•˜์—ฌ append ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  children ์† children์„ ๋˜‘๊ฐ™์ด ๋žœ๋”๋ง์„ ์œ„ํ•ด

์žฌ๊ท€ ์‚ฌ์šฉ! ํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ง ๋‹ค๐Ÿ‘๐Ÿป

 

const menu = [
  {
    type: "group",
    name: "์Œ๋ฃŒ",
    children: [
      {
        type: "group",
        name: "์ฝœ๋“œ ๋ธŒ๋ฃจ",
        children: [
          { type: "item", name: "๋‚˜์ดํŠธ๋กœ ์ฝœ๋“œ ๋ธŒ๋ฃจ" },
          { type: "item", name: "๋Œ์ฒด ์ฝœ๋“œ ๋ธŒ๋ฃจ" },
          { type: "item", name: "์ œ์ฃผ ๋น„์ž๋ฆผ ์ฝœ๋“œ ๋ธŒ๋ฃจ" },
          { type: "item", name: "์ฝœ๋“œ ๋ธŒ๋ฃจ" },
        ],
      },
      {
        type: "group",
        name: "ํ”„๋ผํ‘ธ์น˜๋…ธ",
        children: [
          { type: "item", name: "์• ํ”Œ ์ฟ ํ‚ค ํฌ๋ฆผ ํ”„๋ผํ‘ธ์น˜๋…ธ" },
          { type: "item", name: "๋”๋ธ” ์—์Šคํ”„๋ ˆ์†Œ ์นฉ ํ”„๋ผํ‘ธ์น˜๋…ธ" },
          { type: "item", name: "๋ชจ์นด ํ”„๋ผํ‘ธ์น˜๋…ธ" },
          { type: "item", name: "ํ”ผ์Šคํƒ€์น˜์˜ค ํฌ๋ฆผ ํ”„๋ผํ‘ธ์น˜๋…ธ" },
        ],
      },
			...๋‚˜๋จธ์ง€ ์ƒ๋žต
]

const root = document.getElementById("root");

function createTreeView(menu, currentNode) {
  // TODO: createTreeView ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.
  for (let i of menu) {
    if (i.type === 'group') {

      // li
      let li = document.createElement("li");
      currentNode.append(li);

      // ์ข…๋ฅ˜ ์ด๋ฆ„์€ span
      let span = document.createElement("span");
      span.textContent = i.name;

      // checkbox ์„ค์ •
      let input = document.createElement("input");
      // input.type = "checkbox";
      input.setAttribute("type","checkbox")

      // ul
      let ul = document.createElement("ul");

      // li ์•ˆ์— input, span, ul ์„ ๋‹ด์•„์„œ ํ˜„์žฌ ๋…ธ๋“œ์—๋‹ค๊ฐ€ ๋ถ™์ž„
      li.append(input, span, ul);

      //? ์žฌ๊ท€๋ฅผ ๋Œ๋ฆฐ๋‹ค
      createTreeView(i.children, ul);

    } else {
      //! children ์—†์„ ๋•Œ
      let li = document.createElement('li')
      li.textContent = i.name;
      currentNode.append(li);
    }
  }
}

createTreeView(menu, root);