【问题标题】:unexpected token } in JSON at position 351 while parsing near '..."license": "ISC",JSON 中的意外令牌 } 在位置 351 附近解析 '..."license": "ISC",
【发布时间】:2018-01-02 15:04:12
【问题描述】:

我是 JavaScript 和 JSON 的新手,我正在尝试上传我的 NPM 包。出于某种原因,当我尝试发布它时出现此错误:

Unexpected token } in JSON at position 351 while parsing near '..."license": "ISC",

},
"bugs": {
"e..."

这是我的 JSON 文件。

{
"name": "M-P-Formulas-JS",
"version": "1.0.0",
"description": "The M-P-Formulas package for JavaScript. This package allows the user to use math and physics formulas such as the Pythagorean Theorem.",
"main": "mpformulasJS.js",
"author": {
    "email": "8b21espq@gmail.com",
    "name": "Jeff Lockhart",
    "url": "",
    "license": "ISC",
},
"bugs": {
    "email": "8b21espq@gmail.com",
},
"dependencies": {
    "Math": "",
},
"keywords": [
    "mpformulas",
    "mpformulasjs",
    "m-p-formulas-js",
    "math",
    "physics",
    "formulas",
    "module",
    "package",
 ],
}

我什至跑了npm cache clean,但由于它不起作用,我可以保证我的代码是错误的。如果是这样,我该如何解决?

谢谢。

【问题讨论】:

  • 去掉关键字数组后面的逗号。删除每个尾随逗号
  • 什么代码错了?你的代码在哪里?
  • JSON 不允许在数组的最后一个元素之后使用逗号。
  • 实际上,删除每个对象或数组的最后一行后的尾随逗号。我至少可以看到 5 个。
  • 所有尾随逗号是 JSON 中的语法错误。

标签: javascript json node.js npm


【解决方案1】:

删除多余的逗号,因为它是关闭对象之前的最后一项。这会导致 JSON 无效。

"license": "ISC", // <--- Remove this comma

"bugs": {
    "email": "8b21espq@gmail.com", // <--- Remove this comma
}

JS 对象和 JSON 是不同的。例如,这是一个有效的 JS 对象,但无效的 JSON:

bugs : {
    email : "8b21espq@gmail.com",
}

有效的 JSON 是:

"bugs": {
    "email": "8b21espq@gmail.com"
}

【讨论】:

  • 不客气,如果有用,请考虑验证/投票。
  • "package", 不见了:p
  • 你懂的。
【解决方案2】:

删除右大括号和方括号之前的逗号。仅使用逗号分隔元素。最后一个元素不应有尾随逗号。一些解析器会让你侥幸逃脱,但它是无效的 JavaScript 和 JSON。

Here a tool for testing JSON formatting.

{
"name": "M-P-Formulas-JS",
"version": "1.0.0",
"description": "The M-P-Formulas package for JavaScript. This package allows the user to use math and physics formulas such as the Pythagorean Theorem.",
"main": "mpformulasJS.js",
"author": {
    "email": "8b21espq@gmail.com",
    "name": "Jeff Lockhart",
    "url": "",
    "license": "ISC",              <-- Lose the comma.
},
"bugs": {
    "email": "8b21espq@gmail.com", <-- Lose the comma.
},
"dependencies": {
    "Math": "",                    <-- Lose the comma.
},
"keywords": [
    "mpformulas",
    "mpformulasjs",
    "m-p-formulas-js",
    "math",
    "physics",
    "formulas",
    "module",
    "package",                     <-- Lose the comma.
 ],                                <-- Lose the comma.
}

【讨论】:

  • 在 JSON 中无效,但尾随逗号在 Javascript 中有效已有一段时间了。
  • @jonathan.ihm 是正确的。我的错。 JSON 中当前不允许使用逗号,但 Javascript 中始终允许使用逗号。 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 感谢您添加参考链接,我没有机会 :-) 我只是想澄清一下,因为在某些情况下,尾随逗号实际上是首选!
【解决方案3】:

您的 JSON 在对象末尾有不需要的 ,。所以请尝试下面的代码来删除每个对象的最后一项之后的,

var xx={
"name": "M-P-Formulas-JS",
"version": "1.0.0",
"description": "The M-P-Formulas package for JavaScript. This package allows the user to use math and physics formulas such as the Pythagorean Theorem.",
"main": "mpformulasJS.js",
"author": {
    "email": "8b21espq@gmail.com",
    "name": "Jeff Lockhart",
    "url": "",
    "license": "ISC",
},
"bugs": {
    "email": "8b21espq@gmail.com",
},
"dependencies": {
    "Math": "",
},
"keywords": [
    "mpformulas",
    "mpformulasjs",
    "m-p-formulas-js",
    "math",
    "physics",
    "formulas",
    "module",
    "package",
 ],
}

var xxx=JSON.parse(JSON.stringify(xx).replace('",]','"]').replace('",}','"}'));
console.log(xxx);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-06
    • 2016-12-13
    • 1970-01-01
    • 2013-09-26
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多