我正在try 添加一些数据到一个网站,我想从一个json文件中获取数据.我想要在整个网站上使用这个文件中的数据.
你能推荐一种方法来帮我取一次吗?这样我就不用多次取了.因为原始文件可能包含超过50,000个数据.
我的json就是这种格式,它存储在服务器上的一个名为
Data.json
[
{
"Department": "ABC-1",
"Date": "03-Jan-22",
"Name": "Jane Doe",
"Project": 1,
"Section": 1,
"Task_Code": 999,
"Time": 32,
"Activity_Code": "1_1"
},
{
"Department": "ABC-2",
"Date": "03-Jan-22",
"Name": "Joe Doe",
"Project": 1,
"Section": 1,
"Task_Code": 93,
"Time": 8,
"Activity_Code": "1_1"
},
{
"Department": "ABC-2",
"Date": "03-Jan-22",
"Name": "Jane Doe",
"Project": 1,
"Section": 1,
"Task_Code": 99,
"Time": 8,
"Activity_Code": "1_1"
},
{
"Department": "ABC-1",
"Date": "03-Jan-22",
"Name": "Joe Doe",
"Project": 1,
"Section": 1,
"Task_Code": 98,
"Time": 8,
"Activity_Code": "1_1"
},
{
"Department": "ABC-2",
"Date": "03-Jan-22",
"Name": "Joe Doe",
"Project": 1,
"Section": 1,
"Task_Code": 99,
"Time": 32,
"Activity_Code": "1_1"
},
{
"Department": "ABC-1",
"Date": "03-Jan-22",
"Name": "Jane Doe",
"Project": 1,
"Section": 1,
"Task_Code": 93,
"Time": 8,
"Activity_Code": "1_1"
}
]
我已经写了一个测试hscript.js文件来解析json.我能够获取脚本文件中的文件,但当我try 调用网页中的变量时,它无法识别变量.
index.html
`<!DOCTYPE html>
<html lang="en">
<head>
<!--script src="js/hscript.js"></script-->
</head>
<body>
File Active
<!--script type="module" src="js\index.js"></script-->
<script type="module" src="js/hscript.js"> </script>
<script >
console.log(dataset.length); /variable not returning any value
</script>
</body>`
hscript.js
fetch('../js/Data.json')
.then(response => response.json())
.then(data => console.log(data.length)) //here i get the Console data
当我运行上面的代码时,我会根据代码中的注释获得控制台输出.
我对jQuery的了解很少,请帮忙.