【发布时间】:2018-12-20 10:10:20
【问题描述】:
我正在尝试显示一个“警报”框,其中包含来自 JSON 文件的文本。但似乎我无法从我的 JSON 文件中获取数据。它只是不会抛出警告框。
var thebook = JSON.parse(book);
function showAlert() {
alert(thebook[0].word);
}
<head>
<script type="text/javascript" src="book.json"></script>
</head>
<body>
<button onclick="showAlert()">show alert</button>
</body>
还有一个示例 JSON 文件
[
{
"word" : "cuiller",
"page" : 23
},
{
"word" : "arbre ",
"page" : 245
}
]
【问题讨论】:
-
代码顶部
book的值是多少? -
似乎您尝试以错误的方式加载 json。看看这个:stackoverflow.com/questions/13515141/…
标签: javascript json