JSON (JavaScript Object Notation)

κ°’μ΄λ‚˜ 객체λ₯Ό λ‚˜νƒ€λ‚΄μ£ΌλŠ” λ²”μš© 포맷으둜 jsμ—μ„œ μ‚¬μš©ν•  λͺ©μ μœΌλ‘œ λ§Œλ“€μ–΄μ§„ ν¬λ§·μ΄λ‚˜ ν˜„μž¬ λ‹€λ₯Έ μ–Έμ–΄μ—μ„œλ„ json을 많이 μ΄μš©ν•˜κ³  μžˆλ‹€.

JSON 포맷

  • ν”„λ‘œνΌν‹° 이름(key)κ³Ό κ°’(value)λŠ” ν°λ”°μ˜΄ν‘œλ‘œ κ°μ‹Έμ•Όν•œλ‹€. (μž‘μ€ λ”°μ˜΄ν‘œ x)

  • newλ₯Ό μ‚¬μš©ν•  수 μ—†λ‹€.

κ΄€λ ¨ λ§€μ„œλ“œ

  • JSON.stringify(obj) : 객체λ₯Ό JSON으둜 λ³€ν™˜

    • ν•¨μˆ˜ νŒŒλΌλ―Έν„°

      let json = JSON.stringify(value[, replacer, space])
      • value : 인코딩 ν•˜λ €λŠ” κ°’

        let student = {
          name: 'John',
          age: 30,
          isAdmin: false,
          courses: ['html', 'css', 'js'],
          wife: null,
        };
        let json = JSON.stringify(student);
        
        alert(typeof json); // string
        alert(json);
        /* JSON으둜 μΈμ½”λ”©λœ 객체:
        {
            "name": "John",
            "age": 30,
            "isAdmin": false,
            "courses": ["html", "css", "js"],
            "wife": null
        }
        */
      • replacer : 인코딩 ν•˜κΈΈ μ›ν•˜λŠ” ν”„λ‘œνΌν‹°κ°€ λ‹΄κΈ΄ λ°°μ—΄ or 맀핑 ν•¨μˆ˜

        μˆœν™˜ μ°Έμ‘°λ₯Ό 닀뀄야 ν•˜λŠ” κ²½μš°μ— μ •κ΅ν•˜κ²Œ μ‘°μ •ν•˜κΈ° μœ„ν•΄ μ‚¬μš©

        let room = {
          number: 23,
        };
        
        let meetup = {
          title: 'Conference',
          participants: [{ name: 'John' }, { name: 'Alice' }],
          place: room, // meetup은 room을 μ°Έμ‘°ν•©λ‹ˆλ‹€
        };
        
        room.occupiedBy = meetup; // room은 meetup을 μ°Έμ‘°ν•©λ‹ˆλ‹€
        
        alert(
          JSON.stringify(meetup, function replacer(key, value) {
            alert(`${key}: ${value}`);
            return key == 'occupiedBy' ? undefined : value;
          })
        );
        
        alert(JSON.stringify(meetup, ['title', 'participants', 'place', 'name', 'number']));
      • space : μ„œμ‹ λ³€κ²½ λͺ©μ μœΌλ‘œ μ‚¬μš©ν•  곡백 문자 수

        let user = {
          name: 'John',
          age: 25,
          roles: {
            isAdmin: false,
            isEditor: true,
          },
        };
        
        alert(JSON.stringify(user, null, 2));
        /* 
        {
            "name": "John",
            "age": 25,
            "roles": {
                "isAdmin": false,
                "isEditor": true
            }
        }
        */
        
        alert(JSON.stringify(user, null, 4));
        /*
        {
            "name": "John",
            "age": 25,
            "roles": {
                "isAdmin": false,
                "isEditor": true
            }
        }
        */

    λ³€ν™˜λœ string을 JSON으둜 μΈμ½”λ”©λœ | 직렬화 처리된 | λ¬Έμžμ—΄λ‘œ λ³€ν™˜λœ | κ²°μ§‘λœ 객체라고 λΆ€λ₯Έλ‹€.

    • 객체, λ°°μ—΄, μ›μ‹œμžλ£Œν˜•λ„ μ μš©ν•  수 μžˆλ‹€.

      λ©”μ„œλ“œ, keyκ°€ μ‹¬λ³Όν˜• ν”„λ‘œνΌν‹°, 값이 undefiend인 ν”„λ‘œνΌν‹°λŠ” λ¬΄μ‹œν•œλ‹€.

      • 객체가 μˆœν™˜ μ°Έμ‘°κ°€ μžˆλ‹€λ©΄ λ³€ν™˜μ„ μ‹€νŒ¨ν•œλ‹€. (Error)

  • toJSON() : 객체 내뢀에 ν•¨μˆ˜κ°€ μ •μ˜ λ˜μ–΄ 있으면 JSON.stringify()ν•¨μˆ˜ ν˜ΈμΆœμ‹œ toJSON()이 호좜 λœλ‹€.

  • JSON.parse : JSON을 객체둜 λ³€ν™˜

    • ν•¨μˆ˜ νŒŒλΌλ―Έν„°

      let value = JSON.parse(str, [reviver]);
      • str : JSONν˜•μ‹μ˜ λ¬Έμžμ—΄

      • reviver : λͺ¨λ“  (key,value) μŒμ„ λŒ€μƒμœΌλ‘œ ν˜ΈμΆœλ˜λŠ” function(key,value)ν˜•νƒœμ˜ ν•¨μˆ˜λ‘œ 값을 λ³€κ²½μ‹œν‚¬ 수 μžˆλ‹€.

        μ•„λž˜μ™€ 같이 dateλ₯Ό κ·ΈλŒ€λ‘œ parseμ΄μš©ν•΄ 객체둜 λ³€ν™˜μ‹œ date정보λ₯Ό Date 객체가 μ•„λ‹Œ λ¬Έμžμ—΄λ‘œ λ°›μ•˜κΈ° λ•Œλ¬Έμ— meetup.date.getDate()μ‹œ μ—λŸ¬κ°€ λ°œμƒν•œλ‹€.

        λ”°λΌμ„œ μ•„λž˜μ™€ 같이 reviverλ₯Ό μ΄μš©ν•΄ 객체둜 λ³€ν™˜μ‹œμΌœμ€„ 수 μžˆλ‹€.

        let schedule = `{
            "meetups": [
                {"title":"Conference","date":"2017-11-30T12:00:00.000Z"},
                {"title":"Birthday","date":"2017-04-18T12:00:00.000Z"}
            ]
        }`;
        
        schedule = JSON.parse(schedule, function (key, value) {
          if (key == 'date') return new Date(value);
          return value;
        });
        
        alert(schedule.meetups[1].date.getDate());

Last updated