我可以在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 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 曾经支持评论,但被滥用,从标准中删除。
是从杰森的创造者那里降示的:
我删除了Json的评论, 是因为我看到有人利用他们来维持分解指令, 这种做法会破坏互操作性。 我知道,
json官方网站位于json.org.json被国际电子公司界定为一项标准。 总是有请愿程序要求修订标准。 出于若干原因,说明不可能加入json标准。
json 被设计成一种容易逆向工程(人类割除)的 xml 替代物。 它被简化到无需附加说明的地步。 它甚至不是标记语言。 目标是稳定性和互操作性。
任何理解目标方向“has-a”关系的人,都能理解任何json结构,这就是整个点。这只是一个带有节点标记(键/价值对)的定向环绕图(dag),这是一个近乎普遍的数据结构。
仅此说明要求的可能是“//这些是标记”。 关键名称可以按要求提供信息,允许任意的语义性。
任何平台都可以用几行代码分析json。 xml 需要复杂的 oo 图书馆, 在许多平台上都是不可行的 。
说明只会降低json 的互操作性。 没有什么可以补充的了, 除非您真正需要的是一个标记语言( xml) , 并且不关心您持续的数据是否容易解析 。
但Json的创造者也指出, js一直支持评论:
插入所有您喜欢的评论。 然后通过 Jsmin 管道, 然后再把它交给您的 Json 采摘师 。
2019年视觉演播室代码用户的实际答案是使用“jsonc”扩展名。
这是切合实际的,因为这是视像工作室代码所认可的延伸,表示“Json带评论”,请让我了解下文评论中的其他编辑/旁听者。
如果视觉演播室代码和其他编辑也加入本地对Json5的支持, 那将会是件好事, 但现在视觉演播室代码只包括“jsonc”的支持。
(我在张贴前搜索了所有答案,
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."
}
}
考虑使用 aml 。 它几乎是 json 的超级集( 几乎所有有效的json 都是有效的 aml ) , 它允许评论 。
推荐文章
- 在ASP中设置Access-Control-Allow-OriginNet MVC -最简单的方法
- JWT vs cookie用于基于令牌的身份验证
- JQ:选择多个条件
- 什么是HTTP中的“406-不可接受的响应”?
- 有没有办法注释掉.ASPX页面中的标记?
- ASP。NET Core返回带有状态码的JSON
- 我如何添加环境变量启动。VSCode中的json
- PostgreSQL返回JSON数组的结果集?
- HTTP POST在Java中使用JSON
- 如何指定杰克逊只使用字段-最好是全局
- jQuery为ajax请求返回“parsererror”
- 检查Postgres JSON数组是否包含字符串
- 将bash变量传递给jq
- 在$.ajax()中将数组传递给ajax请求
- 转换JSON字符串到c#对象