在vscode中创建json文件,想要注释一波时,发现报了个错:Comments are not permitted in JSON. (521),意思是JSON中不允许注释
![](https://file.jishuzhan.net/article/1717456367708540929/cc37501243741a31759c7541c982bc95.webp)
以下为解决方法:
在vscode的右下角中找到这个,点击
![](https://file.jishuzhan.net/article/1717456367708540929/ab8e9e259eaa900241d9ad2c4c2bcca7.webp)
在出现的弹窗中输入json with comment,点击搜索出来的jsonc选项即可
但是,需要注意的是,如果要用到json文件中的数据,那么注释会使你请求json时获取的数据变为undefined
![](https://file.jishuzhan.net/article/1717456367708540929/01242d902b8facceb76a9faf64a23a79.webp)
![](https://file.jishuzhan.net/article/1717456367708540929/3fe48f825ca95cae7ed9830e89f698e3.webp)
如果不加注释则显示正常,也能获取其中数据
![](https://file.jishuzhan.net/article/1717456367708540929/fd258a35443b1e7bdf5d9d2e9985208a.webp)
看个人喜好,要真想在json中加注释,也可以尝试使用以下方法
1.用特殊键值表示,虽然有记录,但你不用就行
![](https://file.jishuzhan.net/article/1717456367708540929/4efd6737989e167be5808c3edc644046.webp)
![](https://file.jishuzhan.net/article/1717456367708540929/f743879fea3d724d772d73288de4d5bf.webp)
2.用json5
![](https://file.jishuzhan.net/article/1717456367708540929/8490c3eba3739f6dabd0afb6102afbc0.webp)
![](https://file.jishuzhan.net/article/1717456367708540929/8e1b6b95892b7d6357656fb1ee69f269.webp)
![](https://file.jishuzhan.net/article/1717456367708540929/78b5317213a9e6eb4f6ee30b59f93b5f.webp)
![](https://file.jishuzhan.net/article/1717456367708540929/86a1e94698fa9998910defa6c88c9ed4.webp)