我可以在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对配置文件和其他本地使用非常有意义, 因为它无处不在, 因为它比xml简单得多。
如果人们有强烈的理由反对在传送数据时在json中发表评论(无论是否有效),那么json可能会被分成两个:
json-com: json on the winet, 或在传送json数据时适用的规则。 json- doc: json文件, 或文件或本地文件中的json。 定义有效的json文件的规则 。
json-doc将允许评论,而其他细小差异可能也存在,例如处理白色空间。 剖析器可以很容易地从一个形状转换成另一个形状。
Douglas Crockford就该问题发表的评论(由@artur czajka提及)
假设您正在使用 json 来保存配置文件, 您想要对此进行注释。 请继续, 插入所有您想要的注释 。 然后通过 jsmin 进行管道, 然后将它交给您的 json 采集器 。
我们谈论的是通用配置文件问题(跨语言/平台), 他用js特定工具回答!
可以用任何语言来实施json特定的简化, 但将其标准化, 使得它变得无所不在, 在所有语言和平台的旁听者之间无处不在, 所以人们不再浪费时间来缺少这个功能, 因为他们有很好的使用案例, 在网上论坛里研究这个问题, 并让人们告诉他们这是一个坏主意, 或者说很容易执行删除文本文件的评论。
另一个问题是互操作性。 假设您有图书馆或 api 或任何子系统, 有与其相关的配置或数据文件 。 这个子系统将使用不同语言访问 。 那么您是否要告诉人们: 顺便说一句, 请不要忘记在将 Json 文档中的评论转至 Parser 之前, 请先从 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."
}
}
是的,你可以,但你的分析可能会失败(没有标准)。
要进行剖析,您应该删除这些注释,或者通过手写,或者使用正则表达式:
它替换任何评论,例如:
/****
* Hey
*/
/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/
它替换任何评论,例如:
// Hey
/\/\/.*/
你可以做这样的事情:
jsonString = jsonString.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/, "").replace(/\/\/.*/,"")
var object = JSON.parse(jsonString);
考虑使用 aml 。 它几乎是 json 的超级集( 几乎所有有效的json 都是有效的 aml ) , 它允许评论 。
至少这是我在Json. org的一眼 所经历的
json的语法在那一页上被视觉化了。
推荐文章
- 为什么PHP的json_encode函数转换UTF-8字符串为十六进制实体?
- 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键名中哪些字符是有效的/无效的?