在 VS Code 中,禁用错误“JSON 中不允许评论”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47834825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:04:07  来源:igfitidea点击:

In VS Code, disable error "Comments are not permitted in JSON"

jsonvisual-studio-code

提问by Wallace Kelly

I sometimes use Visual Studio Code to edit JSON files that include comments. VS Code displays an error saying, "Comments are not permitted in JSON." It would be nice to disable that error message (without having to remove the comments.)

我有时会使用 Visual Studio Code 来编辑包含注释的 JSON 文件。VS Code 显示一条错误消息:“JSON 中不允许评论。” 禁用该错误消息会很好(无需删除注释。)

enter image description here

在此处输入图片说明

回答by Wallace Kelly

Follow these steps:

按着这些次序:

  1. Click on the letters JSON in the bottom right corner. (A drop-down will appear to "Select the Language Mode.")
  2. Select "Configure File Association for '.json'..."
  1. 单击右下角的字母 JSON。(将出现一个下拉菜单“选择语言模式”。)
  2. 选择“为'.json'配置文件关联...”

enter image description here

在此处输入图片说明

  1. Type jsoncand press Enter.
  1. 键入jsonc并按 Enter。

enter image description here

在此处输入图片说明

If you only want to disable the error message for a single file, skip step #2.

如果您只想禁用单个文件的错误消息,请跳过步骤 #2。

回答by Joe Maffei

Add this to your User Settings:

将此添加到您的用户设置:

"files.associations": {
    "*.json": "jsonc"
},

If you don't already have a user settings file, you can create one. Hit Ctrl+,or ?+,(that's a comma) to open your settings, then hit the Open Settings (JSON)button in the upper right. It looks like this: the button's icon; a page with a little curved arrow over it

如果您还没有用户设置文件,您可以创建一个。点击Ctrl+,?+,(这是一个逗号)打开您的设置,然后点击右上角的打开设置 (JSON)按钮。它看起来像这样:按钮的图标; 上面有一个小弯曲箭头的页面

回答by v-andrew

Just rename file to test.jsonc

只需将文件重命名为 test.jsonc

Reasons to use JSONCand not to allow comments in the regular JSONfiles are:

JSONC在常规JSON文件中使用和不允许注释的原因是:

  1. It will separate your file from real JSON files
  2. It is not going bite you in the back when you add comment to a file where validation has to be applied but you forget to remove it because there is no error message.
  1. 它将您的文件与真正的 JSON 文件分开
  2. 当您向必须应用验证的文件添加注释但您忘记删除它时,它不会在后面咬您,因为没有错误消息。