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

其他回答

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

json不支持本地的评论, 但您可以自己做解码器或至少预处理器来删除评论, 这完全没问题(只要您只是忽略评论, 不使用它们来指导您的应用程序如何处理json数据 ) 。

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

评论不应被用来传递任何有意义的信息。 这就是json的目的。

参考:杜格拉斯·克罗福德,Json Spec的作者。

没有。

json is only data. 如果您包含一个注释, 那么它也必须是数据 。

您可以有一个名为“_comment”(或某物)的指定数据元素,该元素应该被使用json数据的应用程序忽略。

您也许最好在生成/接收json的进程中得到评论,因为他们应该知道json数据的预发内容,或者至少知道它的结构。

但如果你决定:

{
   "_comment": "comment text goes here...",
   "glossary": {
      "title": "example glossary",
      "GlossDiv": {
         "title": "S",
         "GlossList": {
            "GlossEntry": {
               "ID": "SGML",
               "SortAs": "SGML",
               "GlossTerm": "Standard Generalized Markup Language",
               "Acronym": "SGML",
               "Abbrev": "ISO 8879:1986",
               "GlossDef": {
                  "para": "A meta-markup language, used to create markup languages such as DocBook.",
                  "GlossSeeAlso": ["GML", "XML"]
               },
               "GlossSee": "markup"
            }
         }
      }
   }
}

是, 新的标准json5 允许 c++ 样式注释, 以及许多其他扩展名 :

// A single line comment.

/* A multi-
   line comment. */

json5 数据交换格式 (json5) 是一套超常的json 组合, 旨在减轻json 的某些限制 。 它完全向后兼容, 使用它可能比撰写自定义的非标准采集器、 对现有的打开非标准功能或者使用像字符串字段这样的各种黑客来评论更好 。 或者, 如果使用中的解析器支持的话, 简单地同意我们使用 json 5 子集, 即json 和 c++ 风格评论 。 这比我们We tweak json 标准我们觉得合适的方法要好得多 。

已经存在 npm 软件包、 python 软件包、 java 软件包和 c 库。 它是向后兼容的 。 我看不出有什么理由继续使用“ 官方” json 限制 。

i 认为从json删除评论的原因与删除在java超载的操作员的理由相同: 可能会被错误地使用, 但却忽略了某些明显合理的使用案例。 对于操作员超载, 它就是矩阵代数和复杂数字。 对于 json 的评语, 它是配置文件和其他文件, 可由人类而不是仅由分析员撰写、 编辑或阅读 。

如果您以文本文件的形式装入, 您可以使用带有注释的 json , 然后删除注释 。

例如,您可以为此使用 decomment 库。下面是一个完整的示例。

输入json (文件输入.js) :

/*
* multi-line comments
**/
{
    "value": 123 // one-line comment
}

测试应用程序 :

var decomment = require('decomment');
var fs = require('fs');

fs.readFile('input.js', 'utf8', function (err, data) {
    if (err) {
        console.log(err);
    } else {
        var text = decomment(data); // removing comments
        var json = JSON.parse(text); // parsing JSON
        console.log(json);
    }
});

输出 :

{ value: 123 }

ưμ㼯A