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

JSON的语法在那一页上被视觉化了, 没有任何关于评论的注释。

其他回答

正如许多答复已经指出的,JSON在本地没有评论。当然,有时你还是想要评论。Python 符号,两个方法commentjson (#//仅用于 Python 2 的)或json_tricks (#//Python 2 和 Python 3 的 Python 2 和 Python 3, 具有其他几个特点。json_tricks.

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

希望这有帮助!

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

叹气,为什么不只添加字段,例如:

{
    "note1" : "This demonstrates the provision of annotations within a JSON file",
    "field1" : 12,
    "field2" : "some text",

    "note2" : "Add more annotations as necessary"
}

只要确保你的"笔记本"名字 不要与任何真正的田地冲突。

您可以通过正则表达式使用简单的预处理。例如,以下函数将解码在 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风格的评论: / *, #, / /。 字符串的字句保留不变 。