我能否在以下发言中发表一些意见?Json Json (Json Json) (Json Json Json) (Json Json) Json Json (Json Json) Json (Json) Json (Json) Json (Json) Json (Json) Json (Json) Json (Json) Json(Json) Json (Json) Json (Json) Json(Json) Json (Json) Json如果有,如何处理?


当前回答

JSON的作者想让我们在JSON里加入评论,链接链接如果JSON应该有评论的话,为什么不将其标准化,让JSON专家来做这项工作?我不同意逻辑,但不幸的是,这是标准。使用由其他人建议的YAML解决方案是好的,但它需要依赖图书馆。

如果您想要删除批注, 但不想有库依赖性, 这里是一个双线解决方案, 适用于 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));

其他回答

虽然有些分析者支持C++式的评论,但我使用的是JsonCpp 专案在这些例子中,有这样一个例子:

// Configuration options
{
    // Default encoding for text
    "encoding" : "UTF-8",

    // Plug-ins loaded at start-up
    "plug-ins" : [
        "python",
        "c++",
        "ruby"
        ],

    // Tab indent size
    "indent" : { "length" : 3, "use_space": true }
}

jsonlint (jsonlint)无法验证这一点。 因此, 批注是解析器特定的扩展名, 而不是标准扩展名 。

另一个解析器是杰森5.

JSON 替代 JSON 的替代方案汤加.

另一种替代办法是:jjsonc 喷气管.

最新版本的最新版本nlohmann/json(日元)可选择支持忽略对解析的评论 。

缩略JSON 规格不支持评论,// or /* */样式。

但一些JSON分析图书馆和国际开发实体支持它们。

类似 :

  1. 杰森5
  2. 视觉工作室代码
  3. Commentjson 评论

JSON和JSON需要提出评论。至少可在.NET Core 中提供评论。JSON和牛顿软JSON 工作很顺利

{
  // this is a comment for those who is ok with being different
  "regular-json": "stuff"...
}

我们正在使用strip-json-comments支持我们的项目。 它支持一些东西,比如:

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

简简npm install --save strip-json-comments安装和使用它如下:

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

这是我在Google 火底文件允许你对JSON发表评论:

{
  "//": "Some browsers will use this to enable push notifications.",
  "//": "It is the same for all projects, this is not your project's sender ID",
  "gcm_sender_id": "1234567890"
}