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


当前回答

至少这是我在Json. org的一眼 所经历的

json的语法在那一页上被视觉化了。

其他回答

在撰写本报告时,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.
      }
    }
  }
}

我真的很喜欢 @eli ' s 方法, 有超过 30 个答案, 但没有人提到列表( array) 。 所以使用 @eli ' s 方法, 我们可以做类似的事情:

"part_of_speech": {
  "__comment": [
    "@param {String} type - the following types can be used: ",
      "NOUN, VERB, ADVERB, ADJECTIVE, PRONOUN, PREPOSITION",
      "CONJUNCTION, INTERJECTION, NUMERAL, PARTICLE, PHRASE",
    "@param {String} type_free_form - is optional, can be empty string",
    "@param {String} description - is optional, can be empty string",
    "@param {String} source - is optional, can be empty string"
  ],
  "type": "NOUN",
  "type_free_form": "noun",
  "description": "",
  "source": "https://google.com",
  "noun_class": {
    "__comment": [
      "@param {String} noun_class - the following types can be used: ",
        "1_class, 2_class, 3_class, 4_class, 5_class, 6_class"
    ],
    "noun_class": "4_class"
  }
}

如果您的上下文是节点. js 配置, 您可以考虑通过模块使用 javascript 。 exports 作为json 的替代 :

module.exports = {
    "key": "value",

    // And with comments!
    "key2": "value2"
};

需要的语法将保持不变。 作为 javascript, 文件扩展名应该是 .js 。

它取决于您的 json 库。json.net 支持 javascamps 风格的评论,/ * 逗号 * / 。

见另一个堆叠溢出的问题。

特意从json中删除了评论。

我删除了Json的评论, 是因为我看到人们用他们来维持分析指令, 这种做法会破坏互操作性。 我知道缺乏评论会让一些人感到悲哀, 但不应该。 假设你正在使用 Json 来保存配置文件, 您想要做笔记。 继续, 插入所有您喜欢的评论。 然后通过 jsmin 插入它, 然后把它交给 Jsmin , 然后再交给 Jsson 牧师 。

源代码:由douglas crockford在 g+ 上的公开声明