์ƒˆ์†Œ์‹

Languages/Java Script

[Js] JSON | Ajax์™€ JSON

  • -
๋ฐ˜์‘ํ˜•

๋ชจ๋“  ํฌ์ŠคํŒ…์€ ์ธํ”„๋Ÿฐ [์ƒํ™œ์ฝ”๋”ฉ] ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ•์˜์˜ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์œผ๋ฉฐ

์ถœ์ฒ˜๋Š” ์•„๋ž˜์˜ ์ฃผ์†Œ๋กœ "egoing Lee"๋‹˜์˜ ์ž๋ฃŒ์ž…๋‹ˆ๋‹ค. 

์ƒ์—…์ ์ธ ์˜๋„๊ฐ€ ์•„๋‹Œ ๊ณต๋ถ€ํ•œ ๊ฒƒ์„ ์ •๋ฆฌํ•ด๋†“๋Š” ๋ชฉ์ ์œผ๋กœ ํฌ์ŠคํŒ… ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

www.inflearn.com/course/javascript-%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-%EA%B0%95%EC%A2%8C#

 

์›น๋ธŒ๋ผ์šฐ์ € Javascript (์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ) - ์ธํ”„๋Ÿฐ

์ƒํ™œ์ฝ”๋”ฉ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ•์ขŒ ์ž…๋‹ˆ๋‹ค. ์›น๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ง์ ‘ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๊ณต๋ถ€ํ•ด ๋ด…๋‹ˆ๋‹ค. ์ดˆ๊ธ‰ ์›น ๊ฐœ๋ฐœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด JavaScript ์˜จ๋ผ์ธ ๊ฐ•์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ•์ขŒ

www.inflearn.com


 JSON์ด๋ž€?

JSON(JavaScript Object Notation)์˜ ์•ฝ์ž๋กœ JavaScript์—์„œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ‘œํ˜„์‹์„ ์˜๋ฏธํ•œ๋‹ค.

์ด ํ‘œํ˜„์‹์€ ์‚ฌ๋žŒ๋„ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ณ  ๊ธฐ๊ณ„๋„ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šฐ๋ฉด์„œ ๋ฐ์ดํ„ฐ์˜ ์šฉ๋Ÿ‰์ด ์ž‘๋‹ค.

 

๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” { }์•ˆ์—๋‹ค๊ฐ€ ๊ฐ’์„ ๋„ฃ์–ด์•ผ ๋˜๋ฉฐ, ํ•˜๋‚˜์”ฉ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฒƒ์€ , ๋กœ ํ•œ๋‹ค.

๋˜ํ•œ, ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋Š” ํ‚ค: ๊ฐ’ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค.

 

์ด๋Ÿฐ ์ด์œ ๋กœ ์ตœ๊ทผ์—๋Š” JSON์ด XML์„ ๋Œ€์ฒดํ•ด์„œ ์„ค์ •์˜ ์ €์žฅ์ด๋‚˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†ก๋“ฑ์— ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค. 

 

JSON์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐํ•˜๋ฉด ๋œ๋‹ค.

http://www.json.org/json-ko.html )


 JSON API 

ECMAscript 5์—๋Š” JSON์„ ๊ณต์‹์ ์œผ๋กœ ์ง€์›ํ•˜๋Š” API๊ฐ€ ํฌํ•จ๋˜์—ˆ๋‹ค.

 JSON.parse() 

์ธ์ž๋กœ ์ „๋‹ฌ๋œ ๋ฌธ์‚ฌ์—ด์„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

 JSON.stringify() 

์ธ์ž๋กœ ์ „๋‹ฌ๋œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.


 Ajax์™€ JSON 

JSON์˜ ์ง„๊ฐ€๋Š” ์„œ๋ฒ„์™€ ํ†ต์‹ ์„ ํ•  ๋•Œ ๋“œ๋Ÿฌ๋‚œ๋‹ค.


time.php

<?php
  $timezones = ["Asia/Seoul", "America/New_York"];
  echo implode(',', $timezones);
?>

[ ์‹คํ–‰ ๊ฒฐ๊ณผ ]

Asia/Seoul,America/New_York

์œ„์™€ ๊ฐ™์ด ์„œ๋ฒ„์ชฝ์—์„œ timezones๋ผ๋Š” ๋ฐฐ์—ด์„ ์ฝค๋งˆ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ „๋‹ฌํ•œ๋‹ค.

ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ๋Š” ์ด๋ฅผ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค.


demo2.html

<p id="timezones"></p>
<input type="button" id="execute" value="execute" />
<script>
    document.querySelector('input').addEventListener('click', function(event){
        var xhr = new XMLHttpRequest();
        xhr.open('GET', './time.php');
        xhr.onreadystatechange = function(){
            if(xhr.readyState === 4 && xhr.status === 200){
                var _tzs = xhr.responseText;
                var tzs = _tzs.split(',');
                var _str = '';
                for(var i = 0; i< tzs.length; i++){
                    _str += '<li>'+tzs[i]+'</li>';
                }
                _str = '<ul>'+_str+'</ul>';
                document.querySelector('#timezones').innerHTML = _str;
            }
        }
        xhr.send(); 
    }); 
</script>

์œ„ ์†Œ์Šค ์ฝ”๋“œ์ค‘ ์•„๋ž˜ 3์ค„์˜ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•ด๋ณด์ž.

 

var _tzs = xhr.responseText;
var tzs = _tzs.split(',');
var _str = '';

๋ฉ”์†Œ๋“œ split๋Š” ์ธ์ž์˜ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž๋ฅผ ์ž˜๋ผ์„œ ๋ฐฐ์—ด๋กœ ๋งŒ๋“œ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค.

์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์„œ๋ฒ„์—์„œ ์ „์†กํ•œ Asia/Seoul,America/New_York๋ฅผ split(',')ํ•˜๋ฉด ๋ฐฐ์—ด ['Aasia/Seoul','America/New_York']๊ฐ€ ๋งŒ๋“ค์–ด์ง„๋‹ค.

 

PHP์˜ ๋ฐฐ์—ด์„ ํด๋ผ์ด์–ธํŠธ๋กœ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ฝค๋งˆ๋กœ ๊ตฌ๋ถ„๋œ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค์—ˆ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ์ด๋ฅผ ๋ฐ›์•„์„œ ์ฝค๋งˆ๋ฅผ ๊ตฌ๋ถ„์ž๋กœ ๋‹ค์‹œ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์—ˆ๋‹ค. 


๋งŒ์•ฝ PHP ๋ฐฐ์—ด์„ ๊ทธ๋Œ€๋กœ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฑฐ๋‚˜, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐฐ์—ด์„ ๊ทธ๋Œ€๋กœ PHP์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์–ผ๋งˆ๋‚˜ ํŽธ๋ฆฌํ• ๊นŒ?

์ด๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด JSON์ด๋‹ค.


์œ„ ์˜ˆ์ œ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์•„๋ž˜์—์„œ JSON์œผ๋กœ ๋งŒ๋“ค์–ด๋ณด์ž.

 

time2.php

<?php
    $timezones = ["Asia/Seoul", "America/New_York"];
    header('Content-Type: application/json');
    echo json_encode($timezones);
?>

json_encode๋Š” PHP์˜ ๋ฐ์ดํ„ฐ๋ฅผ JSON ํ˜•์‹์œผ๋กœ ์ „ํ™˜ํ•ด์ฃผ๋Š” PHP์˜ ๋‚ด์žฅํ•จ์ˆ˜๋‹ค.

 

[ ์‹คํ–‰ ๊ฒฐ๊ณผ ]

["Asia\/Seoul","America\/New_York"]

์ด์™€ ๊ฐ™์ด ๋ฐฐ์—ด๋กœ ์ „์†ก๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.


์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” JavaScript ์ฝ”๋“œ๋ฅผ ๋ณด์ž.

<p id="timezones"></p>
<input type="button" id="execute" value="execute" />
<script>
document.querySelector('input').addEventListener('click', function(event){
    var xhr = new XMLHttpRequest();
    xhr.open('GET', './time2.php');
    xhr.onreadystatechange = function(){
        if(xhr.readyState === 4 && xhr.status === 200){
            var _tzs = xhr.responseText;
            var tzs = JSON.parse(_tzs);
            var _str = '';
            for(var i = 0; i< tzs.length; i++){
                _str += '<li>'+tzs[i]+'</li>';
            }
            _str = '<ul>'+_str+'</ul>';
            document.querySelector('#timezones').innerHTML = _str;
        }
    }
    xhr.send(); 
}); 
</script> 

์•„๋ž˜ ์ฝ”๋“œ๋Š” ์„œ๋ฒ„์—์„œ ์ „์†กํ•œ JSON ๋ฐ์ดํ„ฐ๋ฅผ JavaScript์˜ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์ˆ˜ ์žˆ์—ˆ๋‹ค.

var tzs = JSON.parse(_tzs);

 ์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ ์ „์†ก 

์„œ๋ฒ„๋กœ JSON ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ์ฐธ๊ณ ํ•˜์ž.

 

demo4.html

<p>time : <span id="time"></span></p>
<select id="timezone">
    <option value="Asia/Seoul">asia/seoul</option>
    <option value="America/New_York">America/New_York</option>
</select>
<select id="format">
    <option value="Y-m-d H:i:s">Y-m-d H:i:s</option>
    <option value="Y-m-d">Y-m-d</option>
</select>
<input type="button" id="execute" value="execute" />
<script>
document.querySelector('input').addEventListener('click', function(event){
    var xhr = new XMLHttpRequest();
    xhr.open('POST', './time3.php');
    xhr.onreadystatechange = function(){
        document.querySelector('#time').innerHTML = xhr.responseText;
    }
    var data = new Object();
    data.timezone = document.getElementById('timezone').value;
    data.format = document.getElementById('format').value;
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.send(JSON.stringify(data)); 
});
</script>

time3.php

<?php
    $data = json_decode(file_get_contents('php://input'), true);
    $d1 = new DateTime;
    $d1->setTimezone(new DateTimezone($data['timezone']));
    echo $d1->format($data['format']);
?>
๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.