JSON on the Web

jb kim·2021년 11월 6일
0

JSON & AJAX

목록 보기
6/22

JSON.parse()

JSON.parse() 메서드는 JSON 문자열의 구문을 분석하고, 그 결과에서 JavaScript 값이나 객체를 생성합니다.

JSON.parse()

JSON.stringify()

JSON.stringify() 메서드는 JavaScript 값이나 객체를 JSON 문자열로 변환합니다.

JSON.stringify()





많은 웹사이트에서 JSON 데이터를 제공한다.

https://randomuser.me/api/

(JSON 데이터를 보기좋게)
https://jsonlint.com/

<body>
    <h1>Learn JSON</h1>
    <div id="output"></div>
    <script type="text/javascript">
        //https://randomuser.me/api/
        var data = '{"results":[{"gender":"female","name":{"title":"mrs","first":"brandie","last":"harper"},"location":{"street":"6201 w sherman dr","city":"dayton","state":"oklahoma","postcode":62855},"email":"brandie.harper@example.com","login":{"username":"brownbutterfly621","password":"gonzo","salt":"GsbcT8AP","md5":"57cc5b1158d11bb68e899297fef0ebf5","sha1":"0ae0b3af0d2a91754802217f2785f74e93392591","sha256":"4fa6313116c8f3d4d55d79e3a0e8ac7382dfb53d3e5e0b55c89db71f08861296"},"dob":"1992-07-25 23:42:44","registered":"2013-06-20 21:46:22","phone":"(477)-226-7686","cell":"(052)-024-9912","id":{"name":"SSN","value":"210-83-7107"},"picture":{"large":"https://randomuser.me/api/portraits/women/85.jpg","medium":"https://randomuser.me/api/portraits/med/women/85.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/85.jpg"},"nat":"US"}],"info":{"seed":"cb7d885d5ca37ec4","results":1,"page":1,"version":"1.1"}}';
        var dataJSON = JSON.parse(data)
        var person = dataJSON.results[0];
        var output = document.querySelector('#output');
        
        var html = person.name.first + '  ' + person.name.last 
        html += '<img src="'+person.picture.thumbnail+'">';
        output.innerHTML = html;
        
        var strMe = JSON.stringify(dataJSON);
        console.log(strMe);
        
    </script>
</body>
profile
픽서

0개의 댓글