我是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
但是,由于它不起作用,我可以保证我的代码是错误的。如果是这样,我该如何解决?
谢谢。
答案 0 :(得分:2)
删除额外的逗号,因为它是关闭对象之前的最后一项。它导致无效的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"
}
答案 1 :(得分: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.
}
答案 2 :(得分:0)
您的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);