JavaScriptでJsonファイルを読み込み中身を取り出したいのですが、そのときにエラーが出て困っています。
具体的にはのresult.htmlのJSON.parse(this.responseText)で以下の様なエラーが出ています。
Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) at HTMLButtonElement.<anonymous
以下はresult.html、result.js、result.jsonです。
またA君,111,りんご、false、B君、222、ぽち、true、C君、333、車という風に表示したいのですが、どの様にしたら良いのでしょうか。
よろしくお願いします。
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>あああ</title> </head> <body> <div> <h1>Sample</h1> </div> <div><button id="can" type="button">ボタン</button></div> <script type="text/javascript" src="js/result.js"></script> </body> </html>
var xmlHttpRequest; document.getElementById("can").addEventListener("click", function() { xmlHttpRequest = new XMLHttpRequest(); xmlHttpRequest.open("get", "result.json", true); var response = JSON.parse(this.responseText); alert(response.statement[2].name); }, false);
{ "statement":[ {"name":"A君", "data":"111", "word":"りんご"}, {"name":"B君", "data":"222", "word":"ぽち"}, {"name":"C君", "data":"333", "word":"車"} ], "judge":[ false, true ] }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/11 18:03
2017/01/11 18:10