我可以在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"
  }
}

其他回答

正如杰森发明家所说:

json 没有评论。 json 编码器不能输出评论。 json 编码器可以接受和忽略评论。

工具 jq 包括一个解码器,它允许“ ” 式的注释,因此jq 是可与 json- with- comments 文件一起使用的几种工具之一, 只要这些文件被当作“ jq 程序” 处理, 而不是作为 json 文件处理 。 例如 :

$ jq -ncf <(echo $'[1, # one\n2 ] # two') 
[1,2]

更重要的是,jq可以作为程序处理非常大的json-with comments文件;这可以用一个众所周知的json文件来说明:

$ ls -l JEOPARDY_QUESTIONS1.json
-rw-r--r--  2 xyzzy  staff  55554625 May 12  2016 JEOPARDY_QUESTIONS1.json

$ jq -nf JEOPARDY_QUESTIONS1.json | jq length
216930

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

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

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

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

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

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

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

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

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