我尝试使用JS读取这个json文件,但它返回了一个语法错误:第2行出现意外标记":">
这个错误从哪里来?
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"
}
}
您将其解析为文本,而不是JSON。
fetch('../json/destinations_data.json')
.then(response => response.json()) //<-- do this!
.then(data => {
window.alert(data);
console.log(data);
});
您正在读取json文件,因此不使用response.text()
而是使用response.json()
fetch('../json/destinations_data.json')
.then(response => response.json())
.then(data => {
window.alert(data);
console.log(data);
});
json文件中存在语法错误。你必须更改你的格式。请检查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"
}
];
尝试将其解析为json,而不是text
更换这个
.then(response => response.text())
用这个
.then(response => response.json())