我可以在json文件里使用注释吗? 如果是的话,怎么写?
当前回答
注释不是官方标准,尽管有些分析者支持 c++- 风格的注释。
// 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 不验证这一点 。 因此, 批注是解析器特定的扩展名, 而不是标准 。
另一本书是Json5。
取代json Toml的替代品。
另一种选择是jsonc。
Nlohmann/json的最新版本有选择的支持,
其他回答
在我目前的项目中,我遇到了这个问题, 因为我有相当多的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
}
"""))
至少这是我在Json. org的一眼 所经历的
json的语法在那一页上被视觉化了。
没有。
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不支持评论,也从未打算用于需要评论的配置文件。
hjson 是人类的配置文件格式。 放松语法, 减少错误, 更多评论 。
调
图书馆、java、python、php、生锈、走、红宝石、C+++和c图书馆见hjson.github.io。
您可以使用 json-ld 和 schema.org 批注类型来正确写备注:
{
"https://schema.org/comment": "this is a comment"
}
推荐文章
- 在VS Code中禁用“Comments are not allowed In JSON”错误
- 如何在Kotlin解析JSON ?
- JSON文件的蒙古导入
- 如何用jQuery / JavaScript解析JSON数据?
- jQuery在请求体中发布有效的json
- 将JSON显示为HTML
- Swift:理解// MARK
- 在JSON键名中哪些字符是有效的/无效的?
- 如何JSON序列化集?
- 如何检查JSON键是否存在?
- 在构建中编写注释的语法是什么?gradle文件?
- 将对象序列化为JSON
- node.js TypeError:路径必须是绝对路径或指定根路径到res.sendFile[解析JSON失败]
- Python json。loads显示ValueError:额外数据
- JSON省略了Infinity和NaN;JSON在ECMAScript中的状态?