[英]Can't read JSON file using javascript
I have this json file I try to read using JS but it returns a syntax error: unexpected token at line 2 ":"我有这个 json 文件我尝试使用 JS 读取,但它返回语法错误:第 2 行的意外标记“:”
Where could this error come from?这个错误可能来自哪里?
fetch('../json/destinations_data.json')
.then(response => response.text())
.then(data => {
window.alert(data);
console.log(data);
});
{
"user1":{
"email":"lucien.bramare@gmail.com",
"password":"oss117",
"prenom":"Lucien",
"nom":"Bramare"
},
"user2":{
"email":"noel.flantier@gmail.com",
"password":"oss117",
"prenom":"Noël",
"nom":"Flantier"
}
}
You're parsing it as text, not JSON.您将其解析为文本,而不是 JSON。
fetch('../json/destinations_data.json')
.then(response => response.json()) //<-- do this!
.then(data => {
window.alert(data);
console.log(data);
});
Try to parse it as json and not as text尝试将其解析为json而不是文本
replace this替换这个
.then(response => response.text())
with this有了这个
.then(response => response.json())
You are reading json file so instead of using response.text()
use response.json()
您正在阅读 json 文件,所以不要使用
response.text()
使用response.json()
fetch('../json/destinations_data.json')
.then(response => response.json())
.then(data => {
window.alert(data);
console.log(data);
});
There is a syntax error in your json file.您的 json 文件中存在语法错误。 You have to change your format.
你必须改变你的格式。 Please check https://www.w3schools.com/js/js_json.asp
请查看https://www.w3schools.com/js/js_json.asp
users = [
{
"email":"lucien.bramare@gmail.com",
"password":"oss117",
"prenom":"Lucien",
"nom":"Bramare"
},
{
"email":"noel.flantier@gmail.com",
"password":"oss117",
"prenom":"Noël",
"nom":"Flantier"
}
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.