JS에서 사용하기
<body>
<h1>Learn JSON</h1>
<script type="text/javascript">
var person = {
"name": "Joe"
, "last Name": "Miller"
, "car1" : "Mustang"
, "car2" : "F150"
}
var str1= "car";
var cnt = 1;
console.log(person[str1+cnt])
cnt++;
console.log(person[str1+cnt])
</script>
</body>
친구 리스트 화면에 출력
<body>
<h1>Learn JSON</h1>
<div id="output"></div>
<script type="text/javascript">
var myObj = {
"friends": [{
"firstName": "Mike"
, "lastName": "Jones"
},{
"firstName": "John"
, "lastName": "Smith"
} ,{
"firstName": "Laurence"
, "lastName": "Svekis"
},{
"firstName": "Alice"
, "lastName": "James"
}]
}
var users = myObj.friends;
var html = '';
for(var x in users){
html += (Number(x)+1) + ' . ' + users[x].firstName + ' ' + users[x].lastName + '<br>'
}
document.getElementById('output').innerHTML = html;
</script>