我可以在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的最新版本有选择的支持,
其他回答
我们用脱衣配对来做我们的项目。 它支持一些东西,比如:
/*
* Description
*/
{
// rainbows
"unicorn": /* ❤ */ "cake"
}
简单的 npm 安装 -- save 条形引言,用于安装和使用它,例如:
var strip_json_comments = require('strip-json-comments')
var json = '{/*rainbows*/"unicorn":"cake"}';
JSON.parse(strip_json_comments(json));
//=> {unicorn: 'cake'}
没有。
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 的规格之内 。 所以您不应该这样做 。
例如,在这里,我的图像操作系统保存了图像标记和一些基本格式化(注释)信息(底部):
{
"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 没有评论。 json 编码器不能输出评论。 json 编码器可以接受和忽略评论。
工具 jq 包括一个解码器,它允许“ ” 式的注释,因此jq 是可与 json- with- comments 文件一起使用的几种工具之一, 只要这些文件被当作“ jq 程序” 处理, 而不是作为 json 文件处理 。 例如 :
$ jq -ncf <(echo $'[1, # one\n2 ] # two')
[1,2]
更重要的是,jq可以作为程序处理非常大的json-with comments文件;这可以用一个众所周知的json文件来说明:
$ ls -l JEOPARDY_QUESTIONS1.json
-rw-r--r-- 2 xyzzy staff 55554625 May 12 2016 JEOPARDY_QUESTIONS1.json
$ jq -nf JEOPARDY_QUESTIONS1.json | jq length
216930
它取决于您的 json 库。json.net 支持 javascamps 风格的评论,/ * 逗号 * / 。
见另一个堆叠溢出的问题。
推荐文章
- 如何在Typescript中解析JSON字符串
- 错误"Uncaught SyntaxError:意外的标记与JSON.parse"
- 从JSON生成Java类?
- 使用Jackson将Java对象转换为JSON
- Javascript对象Vs JSON
- 在Swift中将字典转换为JSON
- 如何使用新的PostgreSQL JSON数据类型中的字段进行查询?
- 将类实例序列化为JSON
- JSON和对象文字表示法的区别是什么?
- 是否有与XQuery/XPath等价的JSON ?
- 对bash脚本函数中定义的变量使用curl POST
- 从Json转换为类型数组列表<T>
- REST API最佳实践:查询字符串中的参数vs请求体中的参数
- 打印JSON解析对象?
- JSON有查询语言吗?