본문 바로가기

XML & JSON

JSON // 데이터가 2개이상인값 배열로 접근하기

 

	/*  "question": "Which one is correct team name in NBA?" 불러오기 
     question 접근하기 qize -> sport와 maths중 sport선택  -> q1 -> question
     선택사항(key 데이터가 2개이상이면 배열[""]로 접근)  */
	document.getElementById("demo").innerHTML = param2.quiz["sport"].q1.question;

 

 

<body>
<p id="demo"></p>
<script type="text/javascript">
let param2 = {
	    "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"
	            }
	        }
	    }//quiz
	}
	

function jsonTest() {
	/*  "question": "Which one is correct team name in NBA?" 불러오기 
	 question 접근하기 qize -> sport와 maths중 sport선택  -> q1 -> question
	 선택사항(key 데이터가 2개이상이면 배열[""]로 접근) */
	document.getElementById("demo").innerHTML = param2.quiz["sport"].q1.question; 
	
	/* "options"의  "Golden State Warriros"접근하기*/
	document.getElementById("demo").innerHTML = param2.quiz["sport"].q1.options[2];
	
	/* "question": "5 + 7 = ?" */
	document.getElementById("demo").innerHTML = param2.quiz["maths"].q1.question;
	
	/* "11" */
	document.getElementById("demo").innerHTML = param2.quiz["maths"].q1.options[1];

	document.getElementById("demo").innerHTML = param2.quiz["sport"].q1.answer;
}
jsonTest();	//함수실행

</script>
</body>