javascript - Javascript JSON值未定义,但在内存中显示对象?
问题描述
我正在尝试读取 JSON 文件并根据读取的内容设置值。
这是我的leaves.json 的样子:
{
"Button_1": {
"text": "Github",
"link": "https://github.com/"
},
"Button_2": {
"text": "LinkedIn",
"link": "https://www.linkedin.com/"
}
}
这是JSON读取代码:
var buttons_json = fetch('./leaves.json')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.log(error));
我无法从内存中的 JSON 对象中提取值,它告诉我该值未定义。例如:
console.log(buttons_json["Button_1"]["text"]);
未定义。您可以在此处看到控制台输出未定义,但 JSON 对象已正确读取。有任何想法吗?