SyntaxError:JSON中的意外标记}大括号

问题描述 投票:-1回答:2

好吧,出于某种原因,我测试了一些东西只是为了看它是否有效并且它给了我一个错误。我无法弄清楚什么是错的,它看起来很好,我已经比较和搜索了一个小时而没有。我可以在这里做一些非常愚蠢的事情。这是bot.js

const botSettings = require("./botsettings.json");

console.log(botSettings.token);
console.log(botSettings.prefix);

这是package.json

{
  "name": "ultibot",
  "version": "0.0.1",
  "description": "a bot for the discord server The Ritual",
  "main": "bot.js",
  "author": "Rituliant",
  "license": "ISC",
  "dependencies": {
    "discord.js": "^11.3.0"
  }
}

这是botsettings.json

{  
  "token": "thisisnormallyalongstringofrandomletters",
  "prefix": "!",
}

完整的错误就是这个

module.js:665
    throw err;
    ^

SyntaxError: C:\Users\quinb\DiscordBotJS\botsettings.json: Unexpected token } in JSON at position 98
    at JSON.parse (<anonymous>)
    at Object.Module._extensions..json (module.js:662:27)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\Users\quinb\DiscordBotJS\bot.js:1:83)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
json node.js discord
2个回答
0
投票

botsettings.json应该是

{  
  "token": "thisisnormallyalongstringofrandomletters",
  "prefix": "!"
}

即前缀值后没有逗号。


-1
投票

与现代JavaScript不同,JSON不允许在其对象表示法中使用尾随逗号。因此,}中的最终botsettings.json确实出乎意料,因为之前的逗号:

{  
  "token": "thisisnormallyalongstringofrandomletters",
  "prefix": "!",
               ^----- Here
}

如果你删除逗号,那只是有效的JSON:

{  
  "token": "thisisnormallyalongstringofrandomletters",
  "prefix": "!"
}
© www.soinside.com 2019 - 2024. All rights reserved.