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


当前回答

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

/*
 * 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'}

其他回答

如果您正在使用带有 asp. net 的牛顿软性.json 库来读取/删除您可以在 json 内容中使用批注 :

//"名字": "字符串"//"id":

* 这是评论示例 */

ps:单行评论仅用6+版本的牛tonoftjson支持。

给无法从框中思考的人附加注释 : 我使用 json 格式用于 Asp. net 应用程序的基本设置 。 i 读取文件, 将其转换成设置对象 , 并使用 tonoft 库 , 必要时使用 。

我更喜欢在json档案中写关于每个设置的评论, 我真的不在乎json格式的完整性, 只要我使用的图书馆可以使用。

i 认为这比创建单独的“ 设置 ” 设置更容易使用/ 理解 。 读取我的文件, 并解释文件中的设置 。

如果你对这种用法有问题, 对不起, 精灵已经出灯了。 人们会发现 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不支持本地的评论, 但您可以自己做解码器或至少预处理器来删除评论, 这完全没问题(只要您只是忽略评论, 不使用它们来指导您的应用程序如何处理json数据 ) 。

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

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

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

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

/*
 * 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中删除了评论。

我删除了Json的评论, 是因为我看到人们用他们来维持分析指令, 这种做法会破坏互操作性。 我知道缺乏评论会让一些人感到悲哀, 但不应该。 假设你正在使用 Json 来保存配置文件, 您想要做笔记。 继续, 插入所有您喜欢的评论。 然后通过 jsmin 插入它, 然后把它交给 Jsmin , 然后再交给 Jsson 牧师 。

源代码:由douglas crockford在 g+ 上的公开声明