zoukankan      html  css  js  c++  java
  • JavaScript读取json文件

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title></title>
    	</head>
    	<body>
    	</body>
    	<script type="text/javascript">
    		window.onload=function(){
    			var requestUrl='ids.json';
    			var request = new XMLHttpRequest();
    			request.open('GET',requestUrl);
    			request.responseType='text';
    			request.send();
    			request.onload=function(){
    				var idsText=request.response;
    				var ids=JSON.parse(idsText);
    				console.log('输出数组元素:');
    				ids.forEach(function(item,index){
    					console.log(index+":"+item);
    				})
    			}
    		};
    		
    	</script>
    </html>
    
    ids.json:
    
    [
        "11",
        "22",
        "33",
    	"44",
    	"55"
    ]
    
    //加上错误处理
    
    try{
    			window.onload=function(){
    				var requestUrl='ids.json';
    				var request = new XMLHttpRequest();
    				request.open('GET',requestUrl);
    				request.responseType='text';
    				request.send();
    				request.onload=function(){
    					var idsText=request.response;
    					var ids=JSON.parse(idsText);
    					console.log('输出数组元素:');
    					ids.forEach(function(item,index){
    						console.log(index+":"+item);
    					})
    				}
    			};
    		}catch(e){
    			
    		}
    		console.log('I can run here');
  • 相关阅读:
    CSPS模拟 57
    CSPS模拟 56
    CSPS Oct目标
    CSPS模拟 55
    CSPS模拟 54
    CSPS模拟 53
    和manacher有关的乱写
    CSPS模拟 52
    CSPS模拟 51
    Git和代码规范
  • 原文地址:https://www.cnblogs.com/AlexanderZhao/p/12878870.html
Copyright © 2011-2022 走看看