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


当前回答

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

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

其他回答

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

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

正如许多答案已经指出的,Json在本地没有评论。当然,有时你还是想要评论。对python来说,两种方法就是评论json(和//仅对python 2)或json_tricks(或//对python 2和python 3),这有其他几个特点。免责声明:我做了json_tricks。

这是我在Google Firebbase文档中找到的东西, 可以让你在json中发表评论:

{
  "//": "Some browsers will use this to enable push notifications.",
  "//": "It is the same for all projects, this is not your project's sender ID",
  "gcm_sender_id": "1234567890"
}

我真的很喜欢 @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"
  }
}

考虑使用 aml 。 它几乎是 json 的超级集( 几乎所有有效的json 都是有效的 aml ) , 它允许评论 。