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


当前回答

是的,你可以在json档案中发表评论。

{
    "": "Location to post to",
    "postUrl": "https://example.com/upload/",

    "": "Username for basic auth",
    "username": "joebloggs",

    "": "Password for basic auth (note this is in clear, be sure to use HTTPS!",
    "password": "bloejoggs"
}

a 批注只是描述一组代码或配置的目的的文字片段。 由于您可以在 Json 中指定多次密钥, 您可以这样做 。 它在方法上是正确的, 唯一的取舍是您在字典中将有一个空密钥, 并有一些垃圾值( 您可以修饰... ) 。

但我只看到我正在研究的一个项目 是这样做的, 我觉得这是一个非常干净的方法来做它。 享受吧!

其他回答

虽然json不支持评论,jsonc支持。

使用 jsonc 剖析器 。 抱歉, 如果回答为时已晚, 请将您的文件命名为“. jsonc ” 扩展名, 并使用 jsonc 剖析器 。

jsonWithComments.jsonc

例如:

{
    // This is a comment!
    "something": "idk"

}

如果这还不清楚, 我认为机器人是奇怪的。 请在投票前先试一下, 这个问题没有帮助 。

如果你用杰克森做你的杰森采摘师 那么这就是你如何让它 允许评论:

ObjectMapper mapper = new ObjectMapper().configure(Feature.ALLOW_COMMENTS, true);

然后,你可以有这样的评论:

{
  key: "value" // Comment
}

并且也可以从设置开始有评论:

mapper.configure(Feature.ALLOW_YAML_COMMENTS, true);

但一般而言(如以前回答的)规格不允许提出评论。

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

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

我们用脱衣配对来做我们的项目。 它支持一些东西,比如:

/*
 * Description 
*/
{
    // rainbows
    "unicorn": /* ❤ */ "cake"
}

简单的 npm 安装 -- save 条形引言,用于安装和使用它,例如:

var strip_json_comments = require('strip-json-comments')
var json = '{/*rainbows*/"unicorn":"cake"}';
JSON.parse(strip_json_comments(json));
//=> {unicorn: 'cake'}

我搜索了所有页面的答案, 没有人提到关于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