English 中文(简体)
JSON能够使用评论吗?
原标题:Can comments be used in JSON?

Can I use comments inside a JSON file? If so, how?

最佳回答

页: 1

JSON只是数据。 如果你提出评论,那么它也必须是数据。

你可以有一个指定的数据要素,称为_comment>(或某种),而使用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"
            }
         }
      }
   }
}
问题回答

<>No, comments of the form/... or /*...>_. 答案是:

  • https://www.json.org
  • RFC 4627: The application/json Media Type for JavaScript Object Notation (JSON)
  • RFC 8259 The JavaScript Object Notation (JSON) Data Interchange Format (supercedes RFCs 4627, 7158, 7159)

www.un.org/Depts/DGACM/index_spanish.htm 包括如果你选择的话;在分配或传送之前,将其与小型托管人分开。

我刚刚获得<JSON.minification(,其中将评论和白色空间从JSON的一块块中删除,并使JSON能够被划为平地。 因此,你可以这样使用:

JSON.parse(JSON.minify(my_str));

在我发表讲话时,我大声疾呼,有些人甚至不同意这一想法,因此我决定,我就以下原因写一个全面的博客站:的评论是有意义的。 该评论包括“智者”的创建者所作的这一显著评论:

你们正在利用JSON来保存你们想要说明的组合文件。 今后,插入你喜欢的所有评论。 之后,通过伊斯兰法院联盟将此事转交阁下。 - Douglas Crockford, 2012

希望能帮助那些不同意JSON.minification()原因的人。

JSON不支持评论。 在需要评论的情况下,也无意用于配置档案。

Hjson是人类的一种配置文件格式。 Relaxed syntax,更少的错误,更多的评论。

“Hjson

See hjson.github.io for Javagust, Java, Zhu, PHP, Rust, Go、Ruby、C++和C#图书馆。

DISCLAIMER: YOUR WARRANTY ISVOID

正如已经指出的,这一黑板利用了这一幽灵的实施。 并非所有的逊尼派信徒都能理解这种名人。 精简教区将尤其.。

这是一种令人感兴趣的好奇,但你确实不能将其用于所有的任何东西。 以下是最初的答复。


我发现了一个小的黑板,使你能够把评论放在不会影响 par子或以任何方式改变数据代表性的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]
}

上述代码为valid JSON。 如果你宣布,你就获得这样的物品:

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

也就是说,这些评论没有任何痕迹,它们获得的只是副作用。

欢庆!

Consider using YAML. It s nearly a superset of JSON (virtually all valid JSON is valid YAML) and it allows comments.

Comments are not an official standard, although some parsers support C++-style comments. One that I use is JsonCpp. In the examples there is this one:

// 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 does not validate this. So comments are a parser specific extension and not standard.

Another parser is JSON5.

An Alternative to JSON TOML/a。

A further alternative is jsonc.

最新版本nlohmann/json。 选择性地支持无视关于教区的评论。

Here is what I found in the Google Firebase documentation that allows you to put comments in JSON:

{
  "//": "Some browsers will use this to enable push notifications.",
  "//": "It is the same for all projects, this is not your project s sender ID",
  "gcm_sender_id": "1234567890"
}

页: 1 JSON schema目前是一个拟议的因特网规格草案。 除文件外,该表还可用于验证你的初专干事数据。

Example:

{
  "description": "A person",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer",
      "maximum": 125
    }
  }
}

您可以通过使用<>说明>schema属性提供文件。

If you are using Jackson as your JSON parser then this is how you enable it to allow comments:

ObjectMapper mapper = new ObjectMapper().configure(Feature.ALLOW_COMMENTS, true);

然后,你可以提出这样的意见:

{
  key: "value" // Comment
}

也可以从<代码>#开始提出意见,规定:

mapper.configure(Feature.ALLOW_YAML_COMMENTS, true);

但一般来说(如前文所述),说明不允许评论。

如果你使用纽顿软。 Json Library with ASP. 联网读/编辑后,你可以使用JSON内容中的评论意见:

/ “名称”:“指示”

/“id”:t

页: 1 事实

评注*

PS: Single-line comments are only supp或ted with 6+ versions of Newtonsoft Json.

Additional note f或 people who can t think out of the box: I use the JSON f或mat f或 basic settings in an ASP.NET web application I made. I read the file, convert it into the settings object with the Newtonsoft library and use it when necessary.

I prefer writing comments about each individual setting in the JSON file itself, and I really don t care about the integrity of the JSON f或mat as long as the library I use is OK with it.

我认为,这比创造一个单独的环境更容易使用/理解。 README文件并解释其中的环境。

If you have a problem with this kind of usage; s或ry, the genie is out of the lamp. People would find other usages f或 JSON f或mat, and there is nothing you can do about it.

如果你的文本文件是一位智者的话,将由一些方案阅读,那么在使用之前如何排除C或C++风格的评论?

<>Answer>: 它将是一个线人。 如果你这样做,则可将JSON的档案用作配置文件。

JSON背后的想法是在申请之间提供简单的数据交换。 这些语言通常以网络为基础,语言为 Java。

但是,通过评论作为数据中的名称/价值表表肯定会奏效,尽管数据显然需要忽略或具体由分法处理。

尽管如此,这并不是说初专干事档案应当包含传统意义上的评论的意图。 这只是数据。

查阅JSON网站详情。

其他答复称,JSON不支持评论,但部分不真实:作者Douglas Crokford澄清说,只要他们被抛弃,JSON decoder就可以接受评论。

因此,如果你不理会评论,不使用这些评论来指导你的申请如何处理JSON数据,那就完全是罚款和公认的使用案例。 例如,在JSON储存的配置档案中,这一点作为下文“@toolbear”的评论。 显然,由于初专干事主要是指数据传输格式,因此,如果你转交一名有评论意见的初专干事档案,那么在拯救网络带宽之前,最好删除评论。

资料来源:

JSON does not have comments. A JSON encoder MUST NOT output comments. A JSON decoder MAY accept and ignore comments.

Comments should never be used to transmit anything meaningful. That is what JSON is for.

Douglas Crockford, JSON spec, in a post in a court thread in 2005

我刚刚在组合档案中碰到这一点。 我不想使用XML(字面、图形、难以读)、或“ini”格式(无等级、没有实际标准等)或“Properties”格式(如:ini)。

JSON可以做所有工作,但这种工作会减少真实性,更便于阅读,而且在许多语言中,教区很容易和普遍。 它只是一个数据树。 但是,对于“违约”组合和类似组合来说,必须经常记录“违约”组合。 配置绝不是“完整文件”,但树木可保存,必要时可以阅读。

I guess one can use #": “comment”, for “valid” JSON.

JSON对混淆档案和其他地方的其他使用具有许多意义,因为它是普遍的,而且比XML更简单。

If people have strong reasons against having comments in JSON when communicating data (whether valid or not), then possibly JSON could be split into two:

  • JSON-COM: JSON on the wire, or rules that apply when communicating JSON data.
  • JSON-DOC: JSON document, or JSON in files or locally. Rules that define a valid JSON document.

JSON-DOC将允许发表评论,并可能存在其他小差别,例如处理白空间。 囚犯可以很容易地从一个光谱转换到另一个光谱。

关于remark ,由Douglas Crockford就此问题(参见:@Artur Czajka)

你们正在利用JSON来保存你们想要说明的组合文件。 今后,插入你喜欢的所有评论。 之后,通过联邦排雷委员会将其转交阁下。

我们再谈一个通用的集束文档问题(跨语言/插版),他回答一个特别有用的文件!

Sure a JSON specific minify can be implemented in any language, but standardize this so it becomes ubiquitous across parsers in all languages and platforms so people stop wasting their time lacking the feature because they have good use-cases for it, looking the issue up in online forums, and getting people telling them it s a bad idea or suggesting it s easy to implement stripping comments out of text files.

The other issue is interoperability. Suppose you have a library or API or any kind of subsystem which has some config or data files associated with it. And this subsystem is to be accessed from different languages. Then do you go about telling people: by the way don t forget to strip out the comments from the JSON files before passing them to the parser!

如果您使用JSON5 ,你可以包括评论。


JSON5 is a proposed extension to JSON that aims to make it easier for humans to write and maintain by hand. It does this by adding some minimal syntax features directly from ECMAScript 5.

The Dojo Toolkit JavaScript toolkit (at least as of version 1.4), allows you to include comments in your JSON. The comments can be of /* */ format. Dojo Toolkit consumes the JSON via the dojo.xhrGet() call.

其他 Java本工具包也可同样发挥作用。

在选择最后选择之前试验替代数据结构(甚至数据清单)可以有所帮助。

<><>Disapper:> is/str。

确实有增加评论的办法,并停留在具体条款内(不需要额外的教区)。 它不会形成人类可读的评论,而不会有任何区别。

您可以滥用下列内容:

Insignificant whitespace is allowed before or after any token. Whitespace is any sequence of one or more of the following code points: character tabulation (U+0009), line feed (U+000A), carriage return (U+000D), and space (U+0020).

顺便说一句,你可以滥用这一方法增加评论。 例如:开始并用表格结束你的评论。 将该评论编成基3,并使用其他白空间特征来代表它们。 例如。

010212 010202 011000 011000 011010 001012 010122 010121 011021 010202 001012 011022 010212 011020 010202 010202

(hello Base three in ASCII) 但不是零使用空间,而是1个使用线供暖和2个使用载运回。

这只会使你留下许多不可读的白色空间(除非你使电离层光电离层的缩略,把电离层推向该飞地。

我甚至从未这样做过,因为显而易见的原因,也不应该这样做。

JSON本身不容许评论。 理由完全是虚伪的,因为你可以使用JSON it/em>来发表评论,从而完全排除推理,and<>。 完全出于良好理由,将主食数据空间装上 exactly。 这些结果和潜在问题,例如:一名有评论的JSON案。

If you try to put comments in (using // or /* */ or # for instance), then some parsers will fail because this is strictly not within the JSON specification. So you should never do that.

Here, for instance, where my image manipulation system has saved image notations and some basic formatted (comment) information relating to them (at the bottom):

{
    "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
on 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没有定义评论格式。

正如许多人所建议的那样,有一些trick,例如,重复钥匙或具体的关键<代码>_comment,你可以使用。 这 up你们。

这是。 这里的答案是yes>

No, you shouldn t use duplicative object members to stuff side channel data into a JSON encoding. (See "The names within an object SHOULD be unique" in the RFC).

是的,请insert comments around. JSON,你可以做总结。

但是,如果你想把任意的子宫内数据插入并提取给一名有效的JSON,那就是一个答案。 我们利用数据在JSON编码中的非独特代表性。 在《劳动法》第2节中,允许“六种结构性特性之前或之后允许外层空间”的<>。

<<>><>><>><>><><>>> 《区域渔业公约》只规定“六种结构特性中任何一种特性之前或之后,均允许使用外层空间”,但并未明确提及插图、编号、“数字”、“真实”和“null”。 这种遗漏在所有执行中都被忽视。


First, canonicalize your JSON by minifying it:

$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 , "	", $steg);

Here is your output:

$jsonWithComment = $steg . $jsonMin;

就我而言,我需要在初专干事产出之前就把评论用于辩论。 因此,我在 HTTP负责人上做了简要信息,以避免打断客户:

header("My-Json-Comment: Yes, I know it s a workaround ;-) ");

“Entergram

我们正在使用。 用于我们的项目。 它支持诸如:

/*
 * Description 
*/
{
    // rainbows
    "unicorn": /* ❤ */ "cake"
}

www.un.org/Depts/DGACM/index_spanish.htm

var strip_json_comments = require( strip-json-comments )
var json =  {/*rainbows*/"unicorn":"cake"} ;
JSON.parse(strip_json_comments(json));
//=> {unicorn:  cake }




相关问题
VS 10 mangles html comments?

Using the latest VS 10 we created html markup, then commented it with html comments. The file on disk is not mangled, but when it renders, it renders the html comment tags, then removes some of the ...

Comments & OpenSource software [closed]

This may sound like a foolish question or an observation, but i have seen that most of the times when one tries to look at the opensource code, there are no comments or just one or two lines at the ...

Including commented Class declaration in implementation file

Everyone knows the advantages of a more readable code. So in order to make my code more readable what i do normally is include the commented class declaration in the implementation file of that class....

pair programming with comments [closed]

Over the years, I ve discovered that green-programmers tend to read the comments rather than the code to debug issues. Does having one person document the other person s code (and vice-versa) with ...

Commenting JavaScript functions á la Python Docstrings

It is valid JavaScript to write something like this: function example(x) { "Here is a short doc what I do."; // code of the function } The string actually does nothing. Is there any reason, ...