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


当前回答

json 规格不支持评论, // 或/ * * / 样式。

但一些 Json 剖析图书馆和助手支持它们。

类似 :

json5 视觉演播室 代码评论

其他回答

我刚刚找到了"咕噜咕噜咕噜咕噜声"

“从json那里提取评论, 让你在json档案中使用评论!”

{
    // Rainbows
    "unicorn": /* ❤ */ "cake"
}

是的,你可以,但你的分析可能会失败(没有标准)。

要进行剖析,您应该删除这些注释,或者通过手写,或者使用正则表达式:

它替换任何评论,例如:

/****
 * Hey
 */

/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/

它替换任何评论,例如:

// Hey

/\/\/.*/

你可以做这样的事情:

jsonString = jsonString.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/, "").replace(/\/\/.*/,"")
var object = JSON.parse(jsonString);

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

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

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

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

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

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