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


当前回答

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

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

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

其他回答

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

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

* 这是评论示例 */

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

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

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

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

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

如果您的上下文是节点. js 配置, 您可以考虑通过模块使用 javascript 。 exports 作为json 的替代 :

module.exports = {
    "key": "value",

    // And with comments!
    "key2": "value2"
};

需要的语法将保持不变。 作为 javascript, 文件扩展名应该是 .js 。

在我目前的项目中,我遇到了这个问题, 因为我有相当多的json 需要一些评论 使事情容易记住。

i've used this simple python 函数来替换批注并使用 json. loads 将其转换成 dict:

import json, re

def parse_json(data_string):
  result = []
  for line in data_string.split("\n"):
    line = line.strip()
    if len(line) < 1 or line[0:2] == "//":
      continue
    if line[-1] not in "\,\"\'":
      line = re.sub("\/\/.*?$", "", line)
    result.append(line)
  return json.loads("\n".join(result))

print(parse_json("""
{
  // This is a comment
  "name": "value" // so is this
  // "name": "value"
  // the above line gets removed
}
"""))

我们用脱衣配对来做我们的项目。 它支持一些东西,比如:

/*
 * Description 
*/
{
    // rainbows
    "unicorn": /* ❤ */ "cake"
}

简单的 npm 安装 -- save 条形引言,用于安装和使用它,例如:

var strip_json_comments = require('strip-json-comments')
var json = '{/*rainbows*/"unicorn":"cake"}';
JSON.parse(strip_json_comments(json));
//=> {unicorn: 'cake'}

考虑使用 aml 。 它几乎是 json 的超级集( 几乎所有有效的json 都是有效的 aml ) , 它允许评论 。