我能否在以下发言中发表一些意见?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和JSON需要提出评论。至少可在.NET Core 中提供评论。JSON和牛顿软JSON 工作很顺利

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

其他回答

我在目前项目中遇到这个问题,因为我有相当一部分JSON, 需要一些评论才能让事情容易记住。

我用这个简单的 Python 函数替换备注( U)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
}
"""))

在JSON的输出之前,我需要将评论用于调试目的。所以我把调试信息放进HTTP 信头为了避免客户被打破:

header("My-Json-Comment: Yes, I know it's a workaround ;-) ");

Enter image description here

如果您正在使用 NewtonSoft.Json 库和 ASP.NET 来读取/删除您在 JSON 内容中可以使用 :

/"名字": "字符串"

//“ id” : 整数

* 这是

注释示例 */

PS :单行评论仅以6+版本的牛顿软 Json 支持。

给那些无法思考的人的附加说明:我在一个 ASP.NET 网络应用程序中使用了用于基本设置的 JSON 格式。 我读了文件, 将其转换成设置对象, 与 Newtonoft 库一起使用, 必要时使用 。

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

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

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

有一个很好的解决办法(骗局),这是有效的JSON(JSON),但并非在所有情况下都有效,只需两次(或两次以上)设定相同的键。 例如:

{
  "param" : "This is the comment place",
  "param" : "This is value place",
}

所以JSON会理解这一点:

{
  "param" : "This is value place",
}

是的 你可以在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"
}

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

我在很多年前就看到了这个问题 但我只是在一个我正在研究的项目里 看到了这样的事情 我觉得这是个非常干净的方法,享受吧!