[英]Why am I getting a error saying “SyntaxError: unexpected token: ':'” in a JSON file?
Error I am getting:我得到的错误:
SyntaxError: unexpected token: ':' SyntaxError:意外令牌:':'
This is what is in the JSON file:这是 JSON 文件中的内容:
{ "Name":"John", "Age":30 }
This is what is in the JavaScript file reading the data:这是读取数据的 JavaScript 文件中的内容:
var jsonFile = JSON.parse(test);
var Tag = document.getElementById("test");
Tag.innerHTML = jsonFile.Name;
This is the HTML tags I have added in the HTML file:这是我在 HTML 文件中添加的 HTML 标签:
<script type="text/javascript" src="../static/test.json" async></script>
<script type="text/javascript" src="../static/java.js" async></script>
Why am I getting this error?为什么我会收到此错误?
JSON.parse is expecting a string such as JSON.parse 需要一个字符串,例如
'{ "Name":"John", "Age":30 }'
Try the following for reading a local json file尝试以下方法读取本地 json 文件
You can use.json() instead of parse().您可以使用 .json() 代替 parse()。
test.json()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.