简体   繁体   English

为什么我在 JSON 文件中收到错误消息“SyntaxError: unexpected token: ':'”?

[英]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.

相关问题 我正在使用Ionic并收到错误消息:SyntaxError:JSON中的意外标记&lt;在JSON.parse位置0处( <anonymous> ) - I am using ionic and getting an error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) 为什么我在 JSON 中的位置 0 处收到 Unexpected token &lt; 的错误 - Why am I getting an error of Unexpected token < in JSON at position 0 为什么我会收到 Uncaught SyntaxError:意外令牌:标识符 - Why am i getting Uncaught SyntaxError: unexpected token: identifier 为什么我得到:未捕获的SyntaxError:此处出现意外的令牌ILLEGAL - Why am I getting : Uncaught SyntaxError: Unexpected token ILLEGAL here 为什么收到“ Uncaught SyntaxError:JSON输入意外结束”? - Why am I getting 'Uncaught SyntaxError: Unexpected end of JSON input'? 为什么我会收到错误消息:“ SyntaxError:意外令牌&lt;”? - Why i'm getting error: “SyntaxError: Unexpected token <”? 我刚开始在代码学院学习。 我收到错误“ SyntaxError:意外令牌” - I am just starting in code academy. I am getting the error “SyntaxError: Unexpected token” 使用JSON.parse时,出现“ SyntaxError:JSON中位置1的意外令牌” - I am getting “SyntaxError: Unexpected token ' in JSON at position 1” when using JSON.parse 为什么我会收到意外令牌\\ - Why am I am getting Unexpected token \ 我正在尝试发出 AJAX 获取请求,但我不断收到错误“Uncaught SyntaxError: Unexpected token ')'” - I am trying to make an AJAX fetch request and I keep getting an error "Uncaught SyntaxError: Unexpected token ')'"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM