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

其他回答

你可以在jsonp中发表评论,但不能在纯json中发表评论。 我只是花了一个小时试图用这个高雅的图表来做我的节目。

如果你遵循链接,你将看见

?(/* AAPL historical OHLC data from the Google Finance API */
[
/* May 2006 */
[1147651200000,67.79],
[1147737600000,64.98],
...
[1368057600000,456.77],
[1368144000000,452.97]
]);

我本地文件夹里也有类似的文件, 与同一来源的政策没有问题, 所以我决定使用纯json... 当然,$. Getjson因为评论而沉默不语。

我最后向上述地址发送了一份手册 http 请求, 并意识到内容类型是文本/ javascript, 因为, jsonp 返回纯正的 javascript 。 在此情况下, 批注是允许的 。 但我的应用程序返回了内容类型应用程序/ json, 所以我不得不删除批注 。

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

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

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

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

如果您以文本文件的形式装入, 您可以使用带有注释的 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

是, 新的标准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中不允许使用表格///...或/*...* /的注释。

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