我可以在json文件里使用注释吗? 如果是的话,怎么写?


当前回答

因此我将调试信息放入 http 网站页眉, 以避免打破客户端:

header("My-Json-Comment: Yes, I know it's a workaround ;-) ");

其他回答

叹息。为什么不只添加字段,例如:

{
    "note1" : "This demonstrates the provision of annotations within a JSON file",
    "field1" : 12,
    "field2" : "some text",

    "note2" : "Add more annotations as necessary"
}

只要确保你的"笔记本"名字 不与任何真实的字段冲突。

纯粹的答案是否定的。

但有些编辑和平台使用变通办法来补充json的评论。

1. 今天,大多数编辑都有内置选项和扩展选项,以对json文件添加评论。 (例如:- vs code 也有一个带有评论(jsonc) 模式/ vs code 的json 也有一个漂亮的扩展)

在 vscode 中激活 jsonc 模式的链接

2. 有些平台提供内置方式来补充评论(简洁json)。 (例如:-在消防基地,我可以对消防基地.jsons发表一段没有问题的评论。)

    {
    "hosting": {
        "headers": [
            /*{
              "source": "*.html",
              "headers": [
                {
                  "key": "Content-Security-Policy",
                  "value": "default-src 'self' ..."
                }
              ]
            },*/
        ]
      }
    }

3. 在你自己的json 解析方法中,您可以设置一个预定义的密钥名作为注释。

包括:-

     {
        "comment" : "This is a comment",
        "//" :  "This also comment",
        "name" : "This is a real value"
     }

虽然json不支持评论,jsonc支持。

使用 jsonc 剖析器 。 抱歉, 如果回答为时已晚, 请将您的文件命名为“. jsonc ” 扩展名, 并使用 jsonc 剖析器 。

jsonWithComments.jsonc

例如:

{
    // This is a comment!
    "something": "idk"

}

如果这还不清楚, 我认为机器人是奇怪的。 请在投票前先试一下, 这个问题没有帮助 。

在撰写本报告时,Appings.json支持评论。

例如(微软的样本)

{
  "Logging": {
    "LogLevel": { // All providers, LogLevel applies to all the enabled providers.
      "Default": "Error", // Default logging, Error and higher.
      "Microsoft": "Warning" // All Microsoft* categories, Warning and higher.
    },
    "Debug": { // Debug provider.
      "LogLevel": {
        "Default": "Information", // Overrides preceding LogLevel:Default setting.
        "Microsoft.Hosting": "Trace" // Debug:Microsoft.Hosting category.
      }
    },
    "EventSource": { // EventSource provider
      "LogLevel": {
        "Default": "Warning" // All categories of EventSource provider.
      }
    }
  }
}

Dojo工具包 javascript工具包(至少是1.4版) 允许您在 Json 中加入评论。 评论可以是 / * * / 格式。 Dojo 工具包通过 dojo. xhrget () 电话消耗json 。

其它标本工具箱也可以类似地使用。

在选择最后选择之前先试验其他数据结构(甚至数据清单)时,这样做会有帮助。