Data Format - JSON

๋ฐ์ดํ„ฐ

  • ๋ฐ์ดํ„ฐ๋Š” ์ด๋ก ์„ ์„ธ์šฐ๋Š” ๋ฐ ๊ธฐ์ดˆ๊ฐ€ ๋˜๋Š” ์‚ฌ์‹ค, ๋˜๋Š” ๋ฐ”ํƒ•์ด ๋˜๋Š” ์ž๋ฃŒ๋‚˜ ๊ด€์ฐฐ, ์‹คํ—˜, ์กฐ์‚ฌ ๋“ฑ์œผ๋กœ ์–ป์€ ์‚ฌ์‹ค์ด๋‚˜ ์ž๋ฃŒ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

  • ๋ฐ์ดํ„ฐ๋Š” ๋ณต์žกํ•˜๋‹ค. ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›์„ ๋•Œ์—๋Š” ์–ด๋–ค ์•ฝ์†(ํฌ๋งท)์„ ์ •ํ•ด์„œ ์ฃผ๊ณ  ๋ฐ›์•„์•ผ ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ํฌ๋ฉง์—๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค.

๊ฐœ๋…

  • JSON์€ Javascript Object Notation ์˜ ์•ฝ์ž๋กœ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด ๋ฌธ๋ฒ•์œผ๋กœ ๊ตฌ์กฐํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ํ‘œ์ค€ ํฌ๋งท์ด๋‹ค.

  • key-value ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋‚ด๊ณ , ์ค‘๊ฐ„์— ์ฝœ๋ก ์„ ์ด์šฉํ•˜์—ฌ ๊ตฌ๋ถ„ํ•œ๋‹ค. ๊ฐ์ฒด๋Š” ์ค‘๊ด„ํ˜ธ{}๋กœ ๊ฐ์‹ธ์„œ ํ‘œ์‹œํ•œ๋‹ค.

  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด ๋ฌธ๋ฒ•์œผ๋กœ ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€์˜ ํ˜ธํ™˜์„ฑ์ด ์•„์ฃผ ์ข‹๋‹ค.

    • JSON.parse(json_data)

ํƒ€์ž…

  • ๋ฌธ์ž์—ด, ์ˆซ์ž, boolean, object, ๋ฐฐ์—ด, null

//aboutme.json
{
  "name" : "halmuni",
  "age" : 20,
  "favorites" : ["sleeping", "coffee", "coding", "running"],
  "is_married" : false,
  "family" : [
    {
      "relation" : "mother",
      "name" : "kim ok ja"
    },
    {
      "relation" : "father",
      "name" : "hong gil dong"
    },
    {
      "relation" : "sister",
      "name" : "hong duli"
    },
    {
      "relation" : "brother",
      "name" : "hong ttochi"
    }
  ]
}

์ฐธ์กฐ๋ฐฉ๋ฒ•

  • ์œ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

  • aboutme.json ์„ javascript ๊ฐ์ฒด๋กœ ํŒŒ์‹ฑํ•œ ๋’ค,

  • aboutme.js ์˜ ์ด๋ฆ„, ๋‚˜์ด, ์ข‹์•„ํ•˜๋Š” ๊ฒƒ ์ค‘ ์ฒ˜์Œ์˜ ๋‘ ๊ฐ€์ง€ ์š”์†Œ, ๊ฒฐํ˜ผ์—ฌ๋ถ€, ๊ทธ๋ฆฌ๊ณ  ๊ฐ€์กฑ ์ค‘ ์—„๋งˆ์˜ ์ด๋ฆ„์„ ์ฐธ์กฐํ•ด๋ณด์ž.

//aboutme.js
const me = {
  "name" : "halmuni",
  "age" : 20,
  "favorites" : ["sleeping", "coffee", "coding", "running"],
  "is_married" : false,
  "family" : [
    {
      "relation" : "mother",
      "name" : "kim ok ja"
    },
    {
      "relation" : "father",
      "name" : "hong gil dong"
    },
    {
      "relation" : "sister",
      "name" : "hong duli"
    },
    {
      "relation" : "brother",
      "name" : "hong ttochi"
    }
  ]
}

//aboutme.json ์ฐธ์กฐํ•˜๊ธฐ 
console.log(me.name)
console.log(me.age)
console.log(me.favorites[0], me.favorites[1])
console.log(me.is_married)
console.log(me.family[0].name)
console.log(me.family[0]["name"])

์ฃผ์˜ํ•  ์ 

  1. ์˜ค์ง key-value ๋งŒ ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค. undefine๋‚˜ ๋งค์„œ๋“œ๋Š” ๋‹ด์„ ์ˆ˜ ์—†๋‹ค.

  2. ํฐ ๋”ฐ์˜ดํ‘œ๋งŒ์„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

์žฅ์ 

  1. ํ…์ŠคํŠธ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์–ด์„œ, ์‚ฌ๋žŒ๊ณผ ์ปดํ“จํ„ฐ๊ฐ€ ๋ชจ๋‘ ์ฝ๊ณ  ์“ฐ๊ธฐ๊ฐ€ ์‰ฝ๋‹ค.

  2. ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์™€ ํ”Œ๋žซํผ์— ๋…๋ฆฝ์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋กœ ๋‹ค๋ฅธ ์‹œ์Šคํ…œ ๊ฐ„์— ๊ฐ์ฒด๋ฅผ ๊ตํ™˜ํ•˜๊ธฐ์— ์ข‹๋‹ค.

    1. ํ”„๋ก ํŠธ์—์„œ javascript ๋ฅผ ์“ฐ๊ณ , ๋ฐฑ์—”๋“œ์—์„œ java ๋ฅผ ์จ๋„, json ํƒ€์ž…์œผ๋กœ ์‰ฝ๊ฒŒ ์ƒํ˜ธ ๊ตํ™˜์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

    2. ๋•Œ๋ฌธ์— ์ฃผ๋กœ API๋‚˜ config ํŒŒ์ผ์— ํ™œ์šฉ๋œ๋‹ค.

  3. ๊ฐ€๋ณ๋‹ค. ๊ณ„์† ๋‹ซํžŒ ํƒœ๊ทธ๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” xml ๊ณผ ๋น„๊ตํ•˜์—ฌ ๊ฐ€๋ณ๋‹ค.

Last updated