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


当前回答

json speces 并不支持评论, 但是您可以将评论写成密钥来解决这个问题, 比如这样 。

{
  "// my own comment goes here":"",
  "key1":"value 1",

  "// another comment goes here":"",
  "key 2": "value 2 here"
}

我们使用评论文本作为关键, 以确保它们( 几乎) 是独一无二的, 它们不会打破任何解析器。 如果有些评论不是独一无二的, 请在结尾处添加随机数字 。

如果您需要分析任何处理中的评论, 比如剥除这些评论, 您可以用文字填充注释值, 表示它是一个注释 , 比如 :

   {
  "// my own comment goes here" : "_comment",
  "key1":"value 1",

  "// another comment goes here" : "_comment",
  "key 2": "value 2 here"
} 
  

以此方式, 分析者可以找到所有的评论并处理它们 。

其他回答

是的,你可以在json档案中发表评论。

{
    "": "Location to post to",
    "postUrl": "https://example.com/upload/",

    "": "Username for basic auth",
    "username": "joebloggs",

    "": "Password for basic auth (note this is in clear, be sure to use HTTPS!",
    "password": "bloejoggs"
}

a 批注只是描述一组代码或配置的目的的文字片段。 由于您可以在 Json 中指定多次密钥, 您可以这样做 。 它在方法上是正确的, 唯一的取舍是您在字典中将有一个空密钥, 并有一些垃圾值( 您可以修饰... ) 。

但我只看到我正在研究的一个项目 是这样做的, 我觉得这是一个非常干净的方法来做它。 享受吧!

特意从json中删除了评论。

我删除了Json的评论, 是因为我看到人们用他们来维持分析指令, 这种做法会破坏互操作性。 我知道缺乏评论会让一些人感到悲哀, 但不应该。 假设你正在使用 Json 来保存配置文件, 您想要做笔记。 继续, 插入所有您喜欢的评论。 然后通过 jsmin 插入它, 然后把它交给 Jsmin , 然后再交给 Jsson 牧师 。

源代码:由douglas crockford在 g+ 上的公开声明

json不支持评论,也从未打算用于需要评论的配置文件。

hjson 是人类的配置文件格式。 放松语法, 减少错误, 更多评论 。

图书馆、java、python、php、生锈、走、红宝石、C+++和c图书馆见hjson.github.io。

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 字符串, 将会被某个程序读取, 使用前要删除 c 或 c++ 样式的注释会有多困难 ?

回答:如果你这样做,它将是一个班轮。那么json文件可以用作配置文件。