我可以在json文件里使用注释吗? 如果是的话,怎么写?


当前回答

叹息。为什么不只添加字段,例如:

{
    "note1" : "This demonstrates the provision of annotations within a JSON file",
    "field1" : 12,
    "field2" : "some text",

    "note2" : "Add more annotations as necessary"
}

只要确保你的"笔记本"名字 不与任何真实的字段冲突。

其他回答

是, 新的标准json5 允许 c++ 样式注释, 以及许多其他扩展名 :

// A single line comment.

/* A multi-
   line comment. */

json5 数据交换格式 (json5) 是一套超常的json 组合, 旨在减轻json 的某些限制 。 它完全向后兼容, 使用它可能比撰写自定义的非标准采集器、 对现有的打开非标准功能或者使用像字符串字段这样的各种黑客来评论更好 。 或者, 如果使用中的解析器支持的话, 简单地同意我们使用 json 5 子集, 即json 和 c++ 风格评论 。 这比我们We tweak json 标准我们觉得合适的方法要好得多 。

已经存在 npm 软件包、 python 软件包、 java 软件包和 c 库。 它是向后兼容的 。 我看不出有什么理由继续使用“ 官方” json 限制 。

i 认为从json删除评论的原因与删除在java超载的操作员的理由相同: 可能会被错误地使用, 但却忽略了某些明显合理的使用案例。 对于操作员超载, 它就是矩阵代数和复杂数字。 对于 json 的评语, 它是配置文件和其他文件, 可由人类而不是仅由分析员撰写、 编辑或阅读 。

否认声明:你的保证无效

正如已经指出的,这个黑客利用了这个规格的实施。并非所有json的采样师都会理解这种json。特别是流的采样师会窒息。

这是一个有趣的好奇, 但你真的不应该使用它 任何东西。下面是最初的答案。


我发现一个小黑客 允许你在一个 Json 文件中发表评论 不影响解析 或改变数据 以任何方式代表。

似乎当声明对象文字时, 您可以用同一个键指定两个值, 而最后一个值优先 。 信不信由你, 事实证明json 采集器的工作方式是一样的 。 这样我们就可以用它来在源代码json 中创建评论, 这些评论不会出现在解析对象表示中 。

({a: 1, a: 2});
// => Object {a: 2}
Object.keys(JSON.parse('{"a": 1, "a": 2}')).length; 
// => 1

如果我们运用这个技术, 你评论的json文件可能看起来是这样的:

{
  "api_host" : "The hostname of your API server. You may also specify the port.",
  "api_host" : "hodorhodor.com",

  "retry_interval" : "The interval in seconds between retrying failed API calls",
  "retry_interval" : 10,

  "auth_token" : "The authentication token. It is available in your developer dashboard under 'Settings'",
  "auth_token" : "5ad0eb93697215bc0d48a7b69aa6fb8b",

  "favorite_numbers": "An array containing my all-time favorite numbers",
  "favorite_numbers": [19, 13, 53]
}

上面的代码是有效的json。 如果你分析它, 你会得到这样的物体:

{
    "api_host": "hodorhodor.com",
    "retry_interval": 10,
    "auth_token": "5ad0eb93697215bc0d48a7b69aa6fb8b",
    "favorite_numbers": [19,13,53]
}

意思是评论没有痕迹 也不会有怪异的副作用

快乐的黑客!

至少这是我在Json. org的一眼 所经历的

json的语法在那一页上被视觉化了。

还有其他与json兼容的图书馆,这些图书馆支持评论。

一个突出的例子就是“hashcorp语言”(hcl),它是由那些制造流浪者、包装者、领事和保险库的人共同写的。

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."
    }
}