js如何读取json文件
的有关信息介绍如下:js如何读取json文件
先准备一个json文件
使用vscode创建一个html文件
开始撰写js方法
使用原生javascript来处理
先处理读取json文件
之后运行这个html
上一步得到的json就是我们index.json文件中的内容
在开发者工具中就能看到显示的数据
为了便于查看我们json文件的内容显示在html网页中
运行之后的效果,见下图
完成的html代码
window.onload=function(){
varurl="index.json"
varrequest=newXMLHttpRequest();
request.open("get",url);
request.send(null);
request.onload=function(){
if(request.status==200){
varjson=JSON.parse(request.responseText);
//console.log(json);
varol=document.getElementById('ol');
json.person.map(person=>{
varli=document.createElement("li");
li.innerHTML=`名字是${person.name}图片是${person.image}`;
ol.append(li);
})
}
}
}