JSON 对象的尾随逗号有什么问题

Omt*_*guy 3 json visual-studio-code

通常我会像这样创建有效的 JSON 对象:

{
  hasPermission: true,
  notificationStatusId: 1
};
Run Code Online (Sandbox Code Playgroud)

在 VSCode 上,保存文件时,有时会在最后一个属性后面自动添加尾部逗号,如下所示:

{
  hasPermission: true,
  notificationStatusId: 1,
};
Run Code Online (Sandbox Code Playgroud)

实际上我并不是问如何禁用这种行为,而且我知道如何做到这一点。我想问 VSCode 具有自动添加尾随逗号功能的原因是什么?

Mat*_*ner 5

首先,确保文件的语言模式设置为 just JSON(不是JSON with commentsorJSON5JSON takes Manhattan)。状态栏右下角显示语言模式:

JSON语言模式

VS Code 会警告您JSON语言模式下的尾随命令。

至于为什么插入尾随逗号,这可能是由您的扩展之一引起的,因为 VS Code 默认情况下不应执行此操作。尝试检查您的扩展程序以确定是哪个扩展程序导致了此问题