目次 |
---|
・JSONを用いたサンプル ・JSONを用いたサンプルコード ・JSON形式のデータにアクセスする |
data.personals[i].name.escapeHTML()
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script type="text/javascript" src="prototype_1.6.0.js"></script> <script type="text/javascript"> function getPersonal() { new Ajax.Request('sample.json',{method:'get',onComplete:displayResult}); } function displayResult(req) { var data = req.responseText.evalJSON(); var html = '<table border="1">'; html = html + '<tr><th>名前</th><th>年齢</th><th>性別</th></tr>'; for (var i=0; i<data.personals.length;i++) { html = html + '<tr>'; html = html + '<td>' + data.personals[i].name.escapeHTML() + '</td>'; html = html + '<td>' + data.personals[i].age + '</td>'; html = html + '<td>' + data.personals[i].sex.escapeHTML() + '</td>'; html = html + '</tr>'; } html = html + '</html>'; $('result').innerHTML = html; } </script> </head> <body> <input type="button" value="情報を取得する" onclick="getPersonal()"> <div id="result"></div> </body> </html>
var data = [ { "division": "営業部", "date": [ { "080000": "08:00" }, { "090000": "09:00" }, { "100000": "10:00" } ] }, { "division": "技術部", "schedule": [ { "start": "080000" }, { "end": "100000" } ] } ];
for(i in data){ data[i].division; }
<script type="text/javascript"> function json() { var data = [ { "division": "営業部", "date": [ { "a080000": "08:00" }, { "090000": "09:00" }, { "100000": "10:00" } ] }, { "division": "技術部", "schedule": [ { "start": "080000" }, { "end": "100000" } ] } ]; for(i in data){ alert(data[i].division); } alert(data[1].schedule[0].start); alert(data[0].date[0].a080000); } </script>