jQuery.getJSON失败,语法错误

jQuery .getJSON failing, syntax error?

本文关键字:语法 错误 失败 getJSON jQuery      更新时间:2023-09-26

我正在尝试解析一个包含一些简单关键字的JSON文件。我的函数没有运行。通过查看jQuery.getJSON文档,我意识到最有可能的解释是语法错误。我已经非常仔细地研究了我的代码,但我今天才学会JSON。有人能帮我吗?对不起,我知道有很多数据。

JavaScript代码:

$(document).ready(function() {
$.getJSON('edit/key.json', function(data2) {
    console.log(data2);
    //more code
});

});

JSON代码:

{
"RAM": [
    {"term" : "ram"},
    {"term" : "memory"},
    {"term" : "access"},
    {"term" : "random"},
    {"term" : "component"},
    {"term" : "multi-tasking"},
    {"term" : "multi"},
    {"term" : "tasking"},
    {"term" : "gigabyte"},
    {"term" : "temporary"}
],
"Graphics Card": [
    {"term" : "component"},
    {"term" : "graphic"},
    {"term" : "graphics card"},
    {"term" : "processing"},
    {"term" : "processors"},
    {"term" : "unit"},
    {"term" : "card"},
    {"term" : "3D"},
    {"term" : "image"},
    {"term" : "media"},
    {"term" : "gaming"},
    {"term" : "game"},
    {"term" : "nvidia"},
    {"term" : "amd"},
    {"term" : "chip"},
    {"term" : "clock"},
    {"term" : "speed"},
    {"term" : "GPU"},
    {"term" : "video"},
    {"term" : "DVI"},
    {"term" : "DisplayPort"},
    {"term" : "HDMI"}
],
"Ports": [
    {"term" : "port"},
    {"term" : "interface"},
    {"term" : "external"},
    {"term" : "USB"},
    {"term" : "devices"},
    {"term" : "3.0"},
    {"term" : "2.0"},
    {"term" : "VGA"},
    {"term" : "DVI"},
    {"term" : "HDMI"},
    {"term" : "display"},
    {"term" : "port"},
    {"term" : "memory"},
    {"term" : "card"},
    {"term" : "SD"},
    {"term" : "stick"},
    {"term" : "ethernet"},
    {"term" : "input"},
    {"term" : "output"},
],
"CPU": [
    {"term" : "cpu"},
    {"term" : "central"},
    {"term" : "processing"},
    {"term" : "unit"},
    {"term" : "computing"},
    {"term" : "compute"},
    {"term" : "intel"},
    {"term" : "AMD"},
    {"term" : "processors"},
    {"term" : "desktop"},
    {"term" : "laptop"},
    {"term" : "i3"},
    {"term" : "i5"},
    {"term" : "i7"},
    {"term" : "core"},
    {"term" : "turbo"},
    {"term" : "boost"},
    {"term" : "gaming"},
    {"term" : "media"},
    {"term" : "overclocked"},
    {"term" : "clock"},
    {"term" : "speed"}
],
"Hard Drive": [
    {"term" : "store"},
    {"term" : "data"},
    {"term" : "hard"},
    {"term" : "drive"},
    {"term" : "video"},
    {"term" : "music"},
    {"term" : "picture"},
    {"term" : "document"},
    {"term" : "media"},
    {"term" : "processor"},
    {"term" : "gigabyte"},
    {"term" : "terabyte"},
    {"term" : "rpm"},
    {"term" : "cache"}
],
"Operating System": [
    {"term" : "operating"},
    {"term" : "system"},
    {"term" : "os"},
    {"term" : "device"},
    {"term" : "windows"},
    {"term" : "mac"},
    {"term" : "linux"},
    {"term" : "ubuntu"},
    {"term" : "chrome"},
    {"term" : "xp"},
    {"term" : "vista"},
    {"term" : "7"},
    {"term" : "8"},
    {"term" : "home"},
    {"term" : "premium"},
    {"term" : "professional"},
    {"term" : "ultimate"},
]}

您确实有几个语法错误。

端口阵列中有一个额外的逗号。{"term":"output"}后面有一个逗号,它不应该是最后一个元素。

数组的最后一个元素"操作系统"也会发生同样的情况。最后一个元素{"term","final"}后面不应该有逗号。

这是应该正确解析的代码:

{
"RAM": [
    {"term" : "ram"},
    {"term" : "memory"},
    {"term" : "access"},
    {"term" : "random"},
    {"term" : "component"},
    {"term" : "multi-tasking"},
    {"term" : "multi"},
    {"term" : "tasking"},
    {"term" : "gigabyte"},
    {"term" : "temporary"}
],
"Graphics Card": [
    {"term" : "component"},
    {"term" : "graphic"},
    {"term" : "graphics card"},
    {"term" : "processing"},
    {"term" : "processors"},
    {"term" : "unit"},
    {"term" : "card"},
    {"term" : "3D"},
    {"term" : "image"},
    {"term" : "media"},
    {"term" : "gaming"},
    {"term" : "game"},
    {"term" : "nvidia"},
    {"term" : "amd"},
    {"term" : "chip"},
    {"term" : "clock"},
    {"term" : "speed"},
    {"term" : "GPU"},
    {"term" : "video"},
    {"term" : "DVI"},
    {"term" : "DisplayPort"},
    {"term" : "HDMI"}
],
"Ports": [
    {"term" : "port"},
    {"term" : "interface"},
    {"term" : "external"},
    {"term" : "USB"},
    {"term" : "devices"},
    {"term" : "3.0"},
    {"term" : "2.0"},
    {"term" : "VGA"},
    {"term" : "DVI"},
    {"term" : "HDMI"},
    {"term" : "display"},
    {"term" : "port"},
    {"term" : "memory"},
    {"term" : "card"},
    {"term" : "SD"},
    {"term" : "stick"},
    {"term" : "ethernet"},
    {"term" : "input"},
    {"term" : "output"}
],
"CPU": [
    {"term" : "cpu"},
    {"term" : "central"},
    {"term" : "processing"},
    {"term" : "unit"},
    {"term" : "computing"},
    {"term" : "compute"},
    {"term" : "intel"},
    {"term" : "AMD"},
    {"term" : "processors"},
    {"term" : "desktop"},
    {"term" : "laptop"},
    {"term" : "i3"},
    {"term" : "i5"},
    {"term" : "i7"},
    {"term" : "core"},
    {"term" : "turbo"},
    {"term" : "boost"},
    {"term" : "gaming"},
    {"term" : "media"},
    {"term" : "overclocked"},
    {"term" : "clock"},
    {"term" : "speed"}
],
"Hard Drive": [
    {"term" : "store"},
    {"term" : "data"},
    {"term" : "hard"},
    {"term" : "drive"},
    {"term" : "video"},
    {"term" : "music"},
    {"term" : "picture"},
    {"term" : "document"},
    {"term" : "media"},
    {"term" : "processor"},
    {"term" : "gigabyte"},
    {"term" : "terabyte"},
    {"term" : "rpm"},
    {"term" : "cache"}
],
"Operating System": [
    {"term" : "operating"},
    {"term" : "system"},
    {"term" : "os"},
    {"term" : "device"},
    {"term" : "windows"},
    {"term" : "mac"},
    {"term" : "linux"},
    {"term" : "ubuntu"},
    {"term" : "chrome"},
    {"term" : "xp"},
    {"term" : "vista"},
    {"term" : "7"},
    {"term" : "8"},
    {"term" : "home"},
    {"term" : "premium"},
    {"term" : "professional"},
    {"term" : "ultimate"}
]}