본문 바로가기
HTML

39일차//JSON// json파일에서 데이터 이름 찾기(key + value)

by aesup 2021. 3. 9.
728x90
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p id = "demo"></p>

<script type="text/javascript">
let json = {
	    "quiz": {
	        "sport": {
	            "q1": {
	                "question": "Which one is correct team name in NBA?",
	                "options": [
	                    "New York Bulls",
	                    "Los Angeles Kings",
	                    "Golden State Warriros",
	                    "Huston Rocket"
	                ],
	                "answer": "Huston Rocket"
	            }
	        },
	        "maths": {
	            "q1": {
	                "question": "5 + 7 = ?",
	                "options": [
	                    "10",
	                    "11",
	                    "12",
	                    "13"
	                ],
	                "answer": "12"
	            },
	            "q2": {
	                "question": "12 - 8 = ?",
	                "options": [
	                    "1",
	                    "2",
	                    "3",
	                    "4"
	                ],
	                "answer": "4"
	            }
	        }
	    }
	};
	
	function jsonTest() {
		
		document.getElementById("demo").innerHTML
		//=json.quiz["sport"].q1.question;
		//=json.quiz["sport"].options[2];
		
		=json.quiz["maths"].q2.question;
		//quiz는 두개기때문에 배열로 잡혔다
	}
	
	
	jsonTest();

</script>
</body>
</html>
{
    "quiz": {
        "sport": {
            "q1": {
                "question": "Which one is correct team name in NBA?",
                "options": [
                    "New York Bulls",
                    "Los Angeles Kings",
                    "Golden State Warriros",
                    "Huston Rocket"
                ],
                "answer": "Huston Rocket"
            }
        },
        "maths": {
            "q1": {
                "question": "5 + 7 = ?",
                "options": [
                    "10",
                    "11",
                    "12",
                    "13"
                ],
                "answer": "12"
            },
            "q2": {
                "question": "12 - 8 = ?",
                "options": [
                    "1",
                    "2",
                    "3",
                    "4"
                ],
                "answer": "4"
            }
        }
    }
}

728x90