gpt4 book ai didi

javascript - 如何使用 jQuery 从 JSON 读取文件?

转载 作者:行者123 更新时间:2023-11-29 17:24:30 24 4
gpt4 key购买 nike

我正在尝试使用 jQuery 读取 JSON 文件:

       var listOfItems;
$(function() {
$.getJSON('myData.json', function(data) {
listOfItems = data.items;
});
});

这是 myData.json:

{
{"source": "Microsoft", "target": "Amazon", "type": "licensing"},
{"source": "Microsoft", "target": "HTC", "type": "licensing"},
{"source": "Samsung", "target": "Apple", "type": "suit"},
{"source": "Motorola", "target": "Apple", "type": "suit"}
}

但是,Chrome Web Developer 注意到 listOfItems 在此代码后未定义。为什么?我确信 javascript、HTML 和 JSON 文件都位于同一目录中。

最佳答案

修复 JSON。 jsonlint会告诉你哪里出了问题。

JSON 中的属性名称是字符串(不像 JavaScript,它们可能是标识符)所以它们必须被引号

关于javascript - 如何使用 jQuery 从 JSON 读取文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10048546/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com