我能否在以下发言中发表一些意见?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.
});

其他回答

否 无表格、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、表格评论、//…/*…*/在 JSON 中不允许 。 此答案基于 :

  • https://www.json.org
  • RFC 4627: 变化:application/jsonJavaScript 对象标记( JSON) 的介质类型
  • RFC 8259JavaScript 对象标记(JSON)数据交换格式(取代RFCs 4627、7158、7159)

使用杰森5您可以包含注释。


JSON5建议延长JSON5的有效期这样做的方式是直接从ECMAScript 5中增加一些最低限度的语法特征。

这是我在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"
}

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

JSON没有评论。 JSON 编码器必须输入输出评论。 JSON 编码器可能接受和忽略评论。

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

参 :Douglas Crockford,Json Spec的作者,道格拉斯·克罗克福德.

如果您正在使用 PHP, 您可以使用此函数搜索并删除 JSON 字符串中的 / / / * 键入批注, 然后将其解析成对象/ 数组 :

function json_clean_decode($json, $assoc = true, $depth = 512, $options = 0) {
       // search and remove comments like /* */ and //
       $json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);

       if(version_compare(phpversion(), '5.4.0', '>=')) {
           $json = json_decode($json, $assoc, $depth, $options);
       }
       elseif(version_compare(phpversion(), '5.3.0', '>=')) {
           $json = json_decode($json, $assoc, $depth);
       }
       else {
           $json = json_decode($json, $assoc);
       }

       return $json;
   }

希望这有帮助!