我正在使用JavaScript来解析JSON文件.但我无法理解我得到的错误.有谁可以帮我解决这个问题.
**我的代码:Html文件:
<title>JSON Parser</title>
<script type="text/javascript">
function webGLStart() {
var request = new XMLHttpRequest();
request.open("GET","test.json");
var my_JSON_object = JSON.parse(request.responseText);
alert (my_JSON_object.result[0]);
}
</script>
</head>
<body onload="webGLStart();">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
test.json文件:
{"result": [0,1,2,3,4] }
Run Code Online (Sandbox Code Playgroud)
上述代码中的提醒不会在网页上显示任何内容.
它直接使用jQuery:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$.getJSON('test.json', function(data) {
$.each(data, function(key, val) {
console.log("key=" + key + " " + "val=" + val);
});
});
Run Code Online (Sandbox Code Playgroud)
有关更多示例代码,请访问:http://api.jquery.com/jQuery.getJSON/