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


当前回答

如果您正在使用带有 asp. net 的牛顿软性.json 库来读取/删除您可以在 json 内容中使用批注 :

//"名字": "字符串"//"id":

* 这是评论示例 */

ps:单行评论仅用6+版本的牛tonoftjson支持。

给无法从框中思考的人附加注释 : 我使用 json 格式用于 Asp. net 应用程序的基本设置 。 i 读取文件, 将其转换成设置对象 , 并使用 tonoft 库 , 必要时使用 。

我更喜欢在json档案中写关于每个设置的评论, 我真的不在乎json格式的完整性, 只要我使用的图书馆可以使用。

i 认为这比创建单独的“ 设置 ” 设置更容易使用/ 理解 。 读取我的文件, 并解释文件中的设置 。

如果你对这种用法有问题, 对不起, 精灵已经出灯了。 人们会发现 Json 格式的其他用法, 您对此无能为力 。

其他回答

在json中需要评论,并且至少可在.net核心json 和 tonosojson 中提供评论。

{
  // this is a comment for those who is ok with being different
  "regular-json": "stuff"...
}

我真的很喜欢 @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语法 突出的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

2019年视觉演播室代码用户的实际答案是使用“jsonc”扩展名。

这是切合实际的,因为这是视像工作室代码所认可的延伸,表示“Json带评论”,请让我了解下文评论中的其他编辑/旁听者。

如果视觉演播室代码和其他编辑也加入本地对Json5的支持, 那将会是件好事, 但现在视觉演播室代码只包括“jsonc”的支持。

(我在张贴前搜索了所有答案,

是的,你可以,但你的分析可能会失败(没有标准)。

要进行剖析,您应该删除这些注释,或者通过手写,或者使用正则表达式:

它替换任何评论,例如:

/****
 * Hey
 */

/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/

它替换任何评论,例如:

// Hey

/\/\/.*/

你可以做这样的事情:

jsonString = jsonString.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/, "").replace(/\/\/.*/,"")
var object = JSON.parse(jsonString);