我可以在json文件里使用注释吗? 如果是的话,怎么写?


当前回答

我刚刚找到了"咕噜咕噜咕噜咕噜声"

“从json那里提取评论, 让你在json档案中使用评论!”

{
    // Rainbows
    "unicorn": /* ❤ */ "cake"
}

其他回答

是的,你可以在json档案中发表评论。

{
    "": "Location to post to",
    "postUrl": "https://example.com/upload/",

    "": "Username for basic auth",
    "username": "joebloggs",

    "": "Password for basic auth (note this is in clear, be sure to use HTTPS!",
    "password": "bloejoggs"
}

a 批注只是描述一组代码或配置的目的的文字片段。 由于您可以在 Json 中指定多次密钥, 您可以这样做 。 它在方法上是正确的, 唯一的取舍是您在字典中将有一个空密钥, 并有一些垃圾值( 您可以修饰... ) 。

但我只看到我正在研究的一个项目 是这样做的, 我觉得这是一个非常干净的方法来做它。 享受吧!

至少这是我在Json. org的一眼 所经历的

json的语法在那一页上被视觉化了。

*.json文件通常用作配置文件或静态数据,因此需要评论 * 一些像Netbeans这样的编辑在 *.json中接受评论。

问题在于将内容解析到对象上。 解决办法是总是应用清洁功能( 服务器或客户端 ) 。

php /p

 $rgx_arr = ["/\/\/[^\n]*/sim", "/\/\*.*?\*\//sim", "/[\n\r\t]/sim"];
 $valid_json_str = \preg_replace($rgx_arr, '', file_get_contents(path . 'a_file.json'));

手笔

valid_json_str = json_str.replace(/\/\/[^\n]*/gim,'').replace(/\/\*.*?\*\//gim,'')

没有。

json is only data. 如果您包含一个注释, 那么它也必须是数据 。

您可以有一个名为“_comment”(或某物)的指定数据元素,该元素应该被使用json数据的应用程序忽略。

您也许最好在生成/接收json的进程中得到评论,因为他们应该知道json数据的预发内容,或者至少知道它的结构。

但如果你决定:

{
   "_comment": "comment text goes here...",
   "glossary": {
      "title": "example glossary",
      "GlossDiv": {
         "title": "S",
         "GlossList": {
            "GlossEntry": {
               "ID": "SGML",
               "SortAs": "SGML",
               "GlossTerm": "Standard Generalized Markup Language",
               "Acronym": "SGML",
               "Abbrev": "ISO 8879:1986",
               "GlossDef": {
                  "para": "A meta-markup language, used to create markup languages such as DocBook.",
                  "GlossSeeAlso": ["GML", "XML"]
               },
               "GlossSee": "markup"
            }
         }
      }
   }
}

我搜索了所有页面的答案, 没有人提到关于json语法 突出的Github或堆叠溢出, 尽管这个答案已经接近了。

一些 json 剖析器接受 c++- 风格的评论。 在 guthub 或堆叠溢出上写标记时触发它们。 例如, 您可以指定语法加亮类型为 jsonc 。 例如 :

这一点:

```jsonc
// C++-style comment here
{
    "*.md": {
        "softwrap": true
    },
    "colorcolumn": 80,
    "savecursor": true,
    "scrollbar": true,
    "scrollspeed": 5,
    "softwrap": true,
    "wordwrap": true
}
```

生产以下物品:

// C++-style comment here
{
    "*.md": {
        "softwrap": true
    },
    "colorcolumn": 80,
    "savecursor": true,
    "scrollbar": true,
    "scrollspeed": 5,
    "softwrap": true,
    "wordwrap": true
}

作为上文提及的答案,您可以使用惊人的 nlohmann json c++ 库, 分析有 c (/ * * * /) 和 c++ (/) 样式注释的json 。

https://github.com/nlohmann/json https://github.com/nlohmann/json https://github.com/nlohmann/json.

您需要的单页眉文件在此 : https://github.com/nlohmann/json/blob/development/including/nlohmann/json.hpp

图书馆说: https://github. com/nlohmann/jsoncomments-in-json:

json 此库不支持默认的批注 。 它之所以如此, 有三个原因 : 批注不是json 规格的一部分 。 您可以争辩说 / 或 * / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 参数, 您可以通过 参数忽略/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 参数, 参数, 。 。 您可以值 参数忽略参数, 您可以值 。 /\\\\\\\\\\\\\\\\\\\\ 。 参数 。 。 参数值 。 。 。 。 。 参数可以忽略 。 。 。 。 。 。 。 参数将 。 。 。 。 。 参数将 参数将 。 。 。 。 。 。 参数将 注释将 。 。 。 。 。 。 注释将 注释将 。 。 。 。 。 。 。 。 。 。 。 。 注释将 注释将 注释将 注释将 注释将 。 。 注释将 。

json:::parse () 函数。 见 https://json.nlohmann.me/api/basic_json/parse/

参考文献:

https://gist.github.com/damianedwards/31d245773304ca73556?permarink_comment_id=3574928gistcomment-3574928