투케이2K

107. (javascript/자바스크립트) for in 구문을 사용해서 json Object 형태 key , value 확인 실시 본문

JavaScript

107. (javascript/자바스크립트) for in 구문을 사용해서 json Object 형태 key , value 확인 실시

투케이2K 2021. 8. 6. 12:37
반응형

[개발 환경 설정]

개발 툴 : Edit++

개발 언어 : javascript


[소스코드]

    <!-- 내부 JS 지정 : 일반 -->
    <script>

        /*
        [JS 요약 설명]
        1. window.onload : 브라우저 로드 완료 상태를 나타냅니다
        2. json : key, value 를 가지고 있는 데이터 전송 포맷 형태입니다
        3. jsonObject : {key:value} 형태를 가지고 있습니다
        4. jsonArray : [value, value] 형태를 가지고 있습니다
        5. 자바스크립트에서는 중괄호 { } 사용해 json 형식을 지정할 수 있습니다
        */



        /* [html 최초 로드 및 이벤트 상시 대기 실시] */
        window.onload = function() {
            console.log("");
            console.log("[window ready] : [start]");
            console.log("");

            // [이벤트 함수 호출]
            checkJsonObj();
        };



        /* [이벤트 수행 부분] */                
        function checkJsonObj() {
            console.log("");
            console.log("[checkJsonObj] : [start]");
            console.log("");

            // 초기 json object 형식 데이터 선언 실시
            var jsonData = {"one":"하나", "two":"둘"};
            console.log("");
            console.log("[checkJsonObj] : [json 데이터] : " + JSON.stringify(jsonData));
            console.log("");

            // 방법 [1] : json 데이터에 포함된 key 및 value 데이터 확인 실시
            for (var key in jsonData){
                // number 형태로 된 key 데이터도 불러올 수 있습니다
                console.log("[checkJsonObj] : [방법 1] [key] / [value] : " + key + " / " + jsonData[key]);                
            }            

            // 방법 [2] : json key 값을 알고 있는 경우 즉시 호출해서 값을 확인할 수 있습니다 (number 형태 key 값을 확인 불가)
            console.log("");
            console.log("[checkJsonObj] : [방법 2] [key] / [value] : " + "one" + " / " + jsonData.one);
            console.log("[checkJsonObj] : [방법 2] [key] / [value] : " + "two" + " / " + jsonData.two);

        };

    </script>

 


[결과 출력]


[요약 설명]

/*

[JS 요약 설명]

1. window.onload : 브라우저 로드 완료 상태를 나타냅니다

2. json : key, value 를 가지고 있는 데이터 전송 포맷 형태입니다

3. jsonObject : {key:value} 형태를 가지고 있습니다

4. jsonArray : [value, value] 형태를 가지고 있습니다

5. 자바스크립트에서는 중괄호 { } 사용해 json 형식을 지정할 수 있습니다

*/


 

반응형
Comments