我可以在json文件里使用注释吗? 如果是的话,怎么写?
当前回答
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."
}
}
其他回答
虽然json不支持评论,jsonc支持。
使用 jsonc 剖析器 。 抱歉, 如果回答为时已晚, 请将您的文件命名为“. jsonc ” 扩展名, 并使用 jsonc 剖析器 。
jsonWithComments.jsonc
例如:
{
// This is a comment!
"something": "idk"
}
如果这还不清楚, 我认为机器人是奇怪的。 请在投票前先试一下, 这个问题没有帮助 。
确定您可以对 json 进行评论。 从 javascript 读取 json 注释文件, 您可以在解析前删除评论( 参见下面的代码) 。 我确信这个代码可以改进, 但对于使用常规表达式的人来说很容易理解 。
我使用备受评论的json文件来指定我的合成反射系统 的神经元形状。 我也使用备受评论的json 来储存一个运行中的神经元系统的中间状态。 方便地得到评论。 不要听那些告诉你他们是个坏主意的人。
fetch(filename).then(function(response) {
return response.text();
}).then(function(commented) {
return commented.
replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '$1').
replace(/\r/,"\n").
replace(/\n[\n]+/,"\n");
}).then(function(clean) {
return JSON.parse(clean);
}).then(function(json) {
// Do what you want with the JSON object.
});
如果您使用 json5, 您可以包含注释 。
json5建议扩展至json, 目的是使人类更容易手写和保持。
这是一个"你能"的问题。 这是"可以"的答案。
否, 您不应该使用重复对象成员将侧通道数据塞进json 编码 。 (请参见 rfc 中“ 对象中的名称应该是独一无二的 ” ) 。
是的,你可以在json周围插入评论, 你可以分析出来。
但如果你想将任意的侧道数据插入和提取到一个有效的json 中, 请使用一个答案 。 我们利用 Json 编码中的数据非独有的表达方式。 在 rfc 的第二节中, “ 允许在六个结构字符中的任何字符之前或之后使用白色空间 ” 。
* rfc 仅表示“在六个结构字符之前或之后允许有白色空间”,但没有明确提及字符串、数字、“假”、“真”和“null”。 在所有执行中,这一遗漏被忽略。
首先,将你的json拼写成拼写:
$jsonMin = json_encode(json_decode($json));
然后将您的评论编码为二进制 :
$hex = unpack('H*', $comment);
$commentBinary = base_convert($hex[1], 16, 2);
然后输入您的二进制 :
$steg = str_replace('0', ' ', $commentBinary);
$steg = str_replace('1', "\t", $steg);
您的输出 :
$jsonWithComment = $steg . $jsonMin;
json不支持本地的评论, 但您可以自己做解码器或至少预处理器来删除评论, 这完全没问题(只要您只是忽略评论, 不使用它们来指导您的应用程序如何处理json数据 ) 。
json 没有评论。 json 编码器不能输出评论。 json 编码器可以接受和忽略评论。
评论不应被用来传递任何有意义的信息。 这就是json的目的。
参考:杜格拉斯·克罗福德,Json Spec的作者。
推荐文章
- 在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中的状态?