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


当前回答

在json中不允许使用表格///...或/*...* /的注释。

https://www.json.org rfc 4627: Javacampn 对象标记(json)申请/json媒体类型(json) rfc 8259; json) 数据交换格式(supercedes rfcs 4627, 7158, 7159)

其他回答

这是我在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"
}

还有其他与json兼容的图书馆,这些图书馆支持评论。

一个突出的例子就是“hashcorp语言”(hcl),它是由那些制造流浪者、包装者、领事和保险库的人共同写的。

注释中包括您选择的注释;在解析或传输之前用一个分解符将其删除。

i 刚刚发布json.minify () , 它将评论和空白从json区块中分离出来, 并使它成为有效的json, 可以进行剖析。 所以, 您可以使用它 :

JSON.parse(JSON.minify(my_str));

所以我决定写一篇全面的博客文章, 说明为什么评论在json中有意义。

假设您正在使用 json 来保存配置文件, 您想要对这些文件进行注释。 请继续插入您想要的所有评论。 然后通过 jsmin 进行管道, 然后将它交给您的 Jsson 采集器 。 - Douglas crockford, 2012 。

希望这对不同意Json. minify()的原因的人会有帮助。

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

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

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

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