我可以在json文件里使用注释吗? 如果是的话,怎么写?
当前回答
json 不是一个框架化的协议。 它是一种语言自由格式。 所以没有为 json 定义注释格式 。
许多人建议,有些把戏,例如,重复键或特定键的注释,你可以使用。这取决于你。
其他回答
json 的作者希望我们把评论写进json里, 但是在解析之前先把评论删掉( 见 Michael Burr 提供的链接 ) 。 如果json 有评论的话, 为什么不将评论标准化, 让 Json 牧师来做工作? 我不同意这里的逻辑, 但是, 可惜, 这就是标准。 使用其他人建议的山药解决方案是好的, 但是它需要图书馆的依赖性 。
如果您想要删除注释, 但不想有图书馆依赖性, 这里是一个双线解决方案, 用于 c++- 风格的注释, 但可以适应其它选项 :
var comments = new RegExp("//.*", 'mg');
data = JSON.parse(fs.readFileSync(sample_file, 'utf8').replace(comments, ''));
请注意,只有在您可以确定json数据不含评论启动器的情况下,例如('/'),才能使用这一解决方案。
实现 json 解析、 解析评论和不增加图书馆的另一种方法, 就是用 javascript 解释器来评价json 。 当然, 这个方法的提醒是, 您只想要评价未污染的数据( 不可信用户- 输入 ) 。 这是在 node. js 中的一个例子-- 另一个警告, 以下示例将只读过一次数据, 然后它会被缓存 :
data = require(fs.realpathSync(doctree_fp));
如果您的文本文件, 即json 字符串, 将会被某个程序读取, 使用前要删除 c 或 c++ 样式的注释会有多困难 ?
回答:如果你这样做,它将是一个班轮。那么json文件可以用作配置文件。
纯粹的答案是否定的。
但有些编辑和平台使用变通办法来补充json的评论。
1. 今天,大多数编辑都有内置选项和扩展选项,以对json文件添加评论。 (例如:- vs code 也有一个带有评论(jsonc) 模式/ vs code 的json 也有一个漂亮的扩展)
在 vscode 中激活 jsonc 模式的链接
2. 有些平台提供内置方式来补充评论(简洁json)。 (例如:-在消防基地,我可以对消防基地.jsons发表一段没有问题的评论。)
{
"hosting": {
"headers": [
/*{
"source": "*.html",
"headers": [
{
"key": "Content-Security-Policy",
"value": "default-src 'self' ..."
}
]
},*/
]
}
}
3. 在你自己的json 解析方法中,您可以设置一个预定义的密钥名作为注释。
包括:-
{
"comment" : "This is a comment",
"//" : "This also comment",
"name" : "This is a real value"
}
您应该撰写json schema。json schema目前是一份拟议互联网规格草案。除文档外,该schema还可以用于验证您的json数据 。
例如:
{
"description": "A person",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer",
"maximum": 125
}
}
}
您可以使用描述 schema 属性提供文档。
json不允许评论本身。推理是完全愚蠢的, 因为你可以使用json自己来创造评论, 这完全排除了推理, 并且毫无理由地在完全相同的结果和潜在问题上加载采集数据空间, 比如: 带有评论的json文件。
如果您试图在(使用 / 或 / * * * 或 ) 中发表评论, 那么某些剖析器将会失败, 因为严格来说这不在json 的规格之内 。 所以您不应该这样做 。
例如,在这里,我的图像操作系统保存了图像标记和一些基本格式化(注释)信息(底部):
{
"Notations": [
{
"anchorX": 333,
"anchorY": 265,
"areaMode": "Ellipse",
"extentX": 356,
"extentY": 294,
"opacity": 0.5,
"text": "Elliptical area on top",
"textX": 333,
"textY": 265,
"title": "Notation 1"
},
{
"anchorX": 87,
"anchorY": 385,
"areaMode": "Rectangle",
"extentX": 109,
"extentY": 412,
"opacity": 0.5,
"text": "Rect area\non bottom",
"textX": 98,
"textY": 385,
"title": "Notation 2"
},
{
"anchorX": 69,
"anchorY": 104,
"areaMode": "Polygon",
"extentX": 102,
"extentY": 136,
"opacity": 0.5,
"pointList": [
{
"i": 0,
"x": 83,
"y": 104
},
{
"i": 1,
"x": 69,
"y": 136
},
{
"i": 2,
"x": 102,
"y": 132
},
{
"i": 3,
"x": 83,
"y": 104
}
],
"text": "Simple polygon",
"textX": 85,
"textY": 104,
"title": "Notation 3"
}
],
"imageXW": 512,
"imageYW": 512,
"imageName": "lena_std.ato",
"tinyDocs": {
"c01": "JSON image notation data:",
"c02": "-------------------------",
"c03": "",
"c04": "This data contains image notations and related area",
"c05": "selection information that provides a means for an",
"c06": "image gallery to display notations with elliptical,",
"c07": "rectangular, polygonal or freehand area indications",
"c08": "over an image displayed to a gallery visitor.",
"c09": "",
"c10": "X and Y positions are all in image space. The image",
"c11": "resolution is given as imageXW and imageYW, which",
"c12": "you use to scale the notation areas to their proper",
"c13": "locations and sizes for your display of the image,",
"c14": "regardless of scale.",
"c15": "",
"c16": "For Ellipses, anchor is the center of the ellipse,",
"c17": "and the extents are the X and Y radii respectively.",
"c18": "",
"c19": "For Rectangles, the anchor is the top left and the",
"c20": "extents are the bottom right.",
"c21": "",
"c22": "For Freehand and Polygon area modes, the pointList",
"c23": "contains a series of numbered XY points. If the area",
"c24": "is closed, the last point will be the same as the",
"c25": "first, so all you have to be concerned with is drawing",
"c26": "lines between the points in the list. Anchor and extent",
"c27": "are set to the top left and bottom right of the indicated",
"c28": "region, and can be used as a simplistic rectangular",
"c29": "detect for the mouse hover position over these types",
"c30": "of areas.",
"c31": "",
"c32": "The textx and texty positions provide basic positioning",
"c33": "information to help you locate the text information",
"c34": "in a reasonable location associated with the area",
"c35": "indication.",
"c36": "",
"c37": "Opacity is a value between 0 and 1, where .5 represents",
"c38": "a 50% opaque backdrop and 1.0 represents a fully opaque",
"c39": "backdrop. Recommendation is that regions be drawn",
"c40": "only if the user hovers the pointer over the image,",
"c41": "and that the text associated with the regions be drawn",
"c42": "only if the user hovers the pointer over the indicated",
"c43": "region."
}
}
推荐文章
- Ajax会调用什么样的响应,比如'for (;;);{json data}的意思?
- 在JavaScript中将JSON字符串解析为特定对象原型
- 使用Jackson将JSON字符串转换为漂亮的打印JSON输出
- jQuery。由于转义了JSON中的单引号,parseJSON抛出“无效JSON”错误
- 用c#解析JSON
- 如何合并2 JSON对象从2个文件使用jq?
- 在VS Code中禁用“Comments are not allowed In JSON”错误
- 如何在Kotlin解析JSON ?
- JSON文件的蒙古导入
- 如何用jQuery / JavaScript解析JSON数据?
- jQuery在请求体中发布有效的json
- 将JSON显示为HTML
- Swift:理解// MARK
- 在JSON键名中哪些字符是有效的/无效的?
- 如何JSON序列化集?