我能否在以下发言中发表一些意见?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如果有,如何处理?


当前回答

免责声明:这太愚蠢了

事实上,有一种方法可以添加评论,并且不超出规格(不需要额外的分析师 ) 。 它不会在不作任何区分的情况下产生人类可读的评论。

您可滥用下列手段:

任何标志之前或之后都允许不显眼的白色空间。 白色空间是下列一个或多个代码点的任何序列: 字符制表( U+0009)、 线性种子( U+000A)、 运输返回( U+000D) 和空间( U+0020)。

黑客方式, 您可以滥用此选项添加注释 。 例如 : 开始并用标签结束您的评论 。 在 base 3 中编码该注释, 并使用其他空白字符来代表它们 。 例如 。

010212 010202 011000 011000 011010 001012 010122 010121 011021 010202 001012 011022 010212 011020 010202 010202

(hello base three在ASCII)中,但使用空间不是0,而是用于1条线路饲料和2条运输回程。

这将留给您许多无法读取的空白( 除非您用 IDE 插件来编码/ 解码在苍蝇上) 。

我从来没有尝试过这个, 原因很明显,你也不应该。

其他回答

JSON不支持评论,也从未打算用于需要评论的配置文件。

Hjson 是人类的配置文件格式。 放松语法, 减少错误, 更多评论 。

Hjson intro

见见(hjson.github.io) (hjson. github. iio) (hjson. github.) (吉森. 吉图布. io)JavaScript、Java、Python、PHP、Rust、Go、Ruby、C++和C#图书馆的JavaScript、Java、Java、Java、Python、PHP、Rust、Go、Ruby、C++和C#图书馆。

虽然有些分析者支持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(日元)可选择支持忽略对解析的评论 。

您可以通过正则表达式使用简单的预处理。例如,以下函数将解码在 PHP 中评论 JSON:

function json_decode_commented ($data, $objectsAsArrays = false, $maxDepth = 512, $opts = 0) {
  $data = preg_replace('~
    (" (?:[^"\\\\] | \\\\\\\\ | \\\\")*+ ") | \# [^\v]*+ | // [^\v]*+ | /\* .*? \*/
  ~xs', '$1', $data);

  return json_decode($data, $objectsAsArrays, $maxDepth, $opts);
}

它支持所有PHP风格的评论: / *, #, / /。 字符串的字句保留不变 。

你应该写一个杰森计划JSON Schema是目前提议的因特网规格草案,除了文件以外,还可以使用这一规格来验证你的JSON数据。

示例:

{
  "description": "A person",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer",
      "maximum": 125
    }
  }
}

您可以使用以下工具提供文档:描述说明说明说明说明说明说明schema 属性 。

如果您正在使用 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;
   }

希望这有帮助!