我能否在以下发言中发表一些意见?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 进行评论。 要阅读 JavaScript 的JSON 注释文件, 您可以在解析前删除评论( 参见下面的代码 ) 。 我相信这个代码可以改进, 但对于使用常规表达式的人来说很容易理解 。

我用评论的JSON文件来为我的合成反射系统指定神经元形状。我还用评论的JSON来储存一个运行中的神经元系统的中间状态。非常方便有评论。不要听那些告诉你他们是个坏主意的动作。

fetch(filename).then(function(response) {
    return response.text();
}).then(function(commented) {
    return commented.
        replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '$1').
        replace(/\r/,"\n").
        replace(/\n[\n]+/,"\n");
}).then(function(clean) {
    return JSON.parse(clean);
}).then(function(json) {
    // Do what you want with the JSON object.
});

其他回答

我只是为配置文件才看到这个 我不想用XML 数字或“ini”格式(没有等级,没有真正的标准,等等)或Java“Propertys”格式(如.ini)。

JSON可以做他们所能做的一切,但是它不那么多动词,更易为人读——在许多语言中,剖析器很容易,而且无处不在。它只是一棵数据树。但它只是一棵数据树。但是,用“默认”的配置等文件来记录“默认”的配置和类似内容往往是必要的。配置从来不是“完整的文档 ” , 而是需要时可以读取的保存数据树。

我想用得着"#": "comment"为了"活人"JSON

你,你,你能够有评论意见的年份贾索普我只是花了一个小时 试图让程序与例如,Highcharts的这个例子.

如果你跟着链接,你会看到

?(/* 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 手动请求, 并意识到内容类型是text/javascript自此, JSONP 返回纯 JavaScript 。允许但我的应用程序返回了内容类型application/json所以我不得不去掉那些评论

我喜欢 @eli ' s 方法, 超过 30 个答案, 但没有人提到列表( array) 。 所以使用 @eli ' s 方法, 我们可以做一些类似的事情:

"part_of_speech": {
  "__comment": [
    "@param {String} type - the following types can be used: ",
      "NOUN, VERB, ADVERB, ADJECTIVE, PRONOUN, PREPOSITION",
      "CONJUNCTION, INTERJECTION, NUMERAL, PARTICLE, PHRASE",
    "@param {String} type_free_form - is optional, can be empty string",
    "@param {String} description - is optional, can be empty string",
    "@param {String} source - is optional, can be empty string"
  ],
  "type": "NOUN",
  "type_free_form": "noun",
  "description": "",
  "source": "https://google.com",
  "noun_class": {
    "__comment": [
      "@param {String} noun_class - the following types can be used: ",
        "1_class, 2_class, 3_class, 4_class, 5_class, 6_class"
    ],
    "noun_class": "4_class"
  }
}

如果您正在使用杰克逊JSON作为您的 JSON 分析者, 那么这就是你如何 允许它 允许评论:

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

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

{
  key: "value" // Comment
}

您也可以从#通过设定:

mapper.configure(Feature.ALLOW_YAML_COMMENTS, true);

但总的来说(如前所述),规格不允许作出评论。

有一个很好的解决办法(骗局),这是有效的JSON(JSON),但并非在所有情况下都有效,只需两次(或两次以上)设定相同的键。 例如:

{
  "param" : "This is the comment place",
  "param" : "This is value place",
}

所以JSON会理解这一点:

{
  "param" : "This is value place",
}