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


当前回答

json 的作者希望我们把评论写进json里, 但是在解析之前先把评论删掉( 见 Michael Burr 提供的链接 ) 。 如果json 有评论的话, 为什么不将评论标准化, 让 Json 牧师来做工作? 我不同意这里的逻辑, 但是, 可惜, 这就是标准。 使用其他人建议的山药解决方案是好的, 但是它需要图书馆的依赖性 。

如果您想要删除注释, 但不想有图书馆依赖性, 这里是一个双线解决方案, 用于 c++- 风格的注释, 但可以适应其它选项 :

var comments = new RegExp("//.*", 'mg');
data = JSON.parse(fs.readFileSync(sample_file, 'utf8').replace(comments, ''));

请注意,只有在您可以确定json数据不含评论启动器的情况下,例如('/'),才能使用这一解决方案。

实现 json 解析、 解析评论和不增加图书馆的另一种方法, 就是用 javascript 解释器来评价json 。 当然, 这个方法的提醒是, 您只想要评价未污染的数据( 不可信用户- 输入 ) 。 这是在 node. js 中的一个例子-- 另一个警告, 以下示例将只读过一次数据, 然后它会被缓存 :

data = require(fs.realpathSync(doctree_fp));

其他回答

在我目前的项目中,我遇到了这个问题, 因为我有相当多的json 需要一些评论 使事情容易记住。

i've used this simple python 函数来替换批注并使用 json. loads 将其转换成 dict:

import json, re

def parse_json(data_string):
  result = []
  for line in data_string.split("\n"):
    line = line.strip()
    if len(line) < 1 or line[0:2] == "//":
      continue
    if line[-1] not in "\,\"\'":
      line = re.sub("\/\/.*?$", "", line)
    result.append(line)
  return json.loads("\n".join(result))

print(parse_json("""
{
  // This is a comment
  "name": "value" // so is this
  // "name": "value"
  // the above line gets removed
}
"""))

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

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

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

是, 新的标准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语法 突出的Github或堆叠溢出, 尽管这个答案已经接近了。

一些 json 剖析器接受 c++- 风格的评论。 在 guthub 或堆叠溢出上写标记时触发它们。 例如, 您可以指定语法加亮类型为 jsonc 。 例如 :

这一点:

```jsonc
// C++-style comment here
{
    "*.md": {
        "softwrap": true
    },
    "colorcolumn": 80,
    "savecursor": true,
    "scrollbar": true,
    "scrollspeed": 5,
    "softwrap": true,
    "wordwrap": true
}
```

生产以下物品:

// C++-style comment here
{
    "*.md": {
        "softwrap": true
    },
    "colorcolumn": 80,
    "savecursor": true,
    "scrollbar": true,
    "scrollspeed": 5,
    "softwrap": true,
    "wordwrap": true
}

作为上文提及的答案,您可以使用惊人的 nlohmann json c++ 库, 分析有 c (/ * * * /) 和 c++ (/) 样式注释的json 。

https://github.com/nlohmann/json https://github.com/nlohmann/json https://github.com/nlohmann/json.

您需要的单页眉文件在此 : https://github.com/nlohmann/json/blob/development/including/nlohmann/json.hpp

图书馆说: https://github. com/nlohmann/jsoncomments-in-json:

json 此库不支持默认的批注 。 它之所以如此, 有三个原因 : 批注不是json 规格的一部分 。 您可以争辩说 / 或 * / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 参数, 您可以通过 参数忽略/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 参数, 参数, 。 。 您可以值 参数忽略参数, 您可以值 。 /\\\\\\\\\\\\\\\\\\\\ 。 参数 。 。 参数值 。 。 。 。 。 参数可以忽略 。 。 。 。 。 。 。 参数将 。 。 。 。 。 参数将 参数将 。 。 。 。 。 。 参数将 注释将 。 。 。 。 。 。 注释将 注释将 。 。 。 。 。 。 。 。 。 。 。 。 注释将 注释将 注释将 注释将 注释将 。 。 注释将 。

json:::parse () 函数。 见 https://json.nlohmann.me/api/basic_json/parse/

参考文献:

https://gist.github.com/damianedwards/31d245773304ca73556?permarink_comment_id=3574928gistcomment-3574928

json speces 并不支持评论, 但是您可以将评论写成密钥来解决这个问题, 比如这样 。

{
  "// my own comment goes here":"",
  "key1":"value 1",

  "// another comment goes here":"",
  "key 2": "value 2 here"
}

我们使用评论文本作为关键, 以确保它们( 几乎) 是独一无二的, 它们不会打破任何解析器。 如果有些评论不是独一无二的, 请在结尾处添加随机数字 。

如果您需要分析任何处理中的评论, 比如剥除这些评论, 您可以用文字填充注释值, 表示它是一个注释 , 比如 :

   {
  "// my own comment goes here" : "_comment",
  "key1":"value 1",

  "// another comment goes here" : "_comment",
  "key 2": "value 2 here"
} 
  

以此方式, 分析者可以找到所有的评论并处理它们 。