如何读取JSON信息



所以我有一些代码,我想要它,这样我的JavaScript代码就会读取JSON信息,然后让他们访问人员面板。这是我的JSON:

{
"User1": {
"User": "Elliott",
"StaffPanelAdvanced": "true"
},
"User2": {
"User": "Max",
"StaffPanelAdvanced": "true"
},
"Password:": "2201-hrstaffpanel"
}

这是我的JavaScript:

let pr = prompt("What is your staff name? (Case Sensitive)")
if (pr === data.User1.User) {
alert("Logged in successfully, " + pr + ".")
}

(顺便说一句,我使用"fetch"来获取JSON信息,因为"require"不起作用。(为什么这不起作用?

如果使用fetch,请确保将JSON响应解析为对象。

fetch('url')
.then((response) => {
if (!response.ok) {
throw new Error(response.statusText)
}
return response.json() // Parse JSON response
})
.then((data) => {
// You can access data as object now.
console.log(data)
})

最新更新