我能否在以下发言中发表一些意见?Json Json (Json Json) (Json Json Json) (Json Json) Json Json (Json Json) Json (Json) Json (Json) Json (Json) Json (Json) Json (Json) Json (Json) Json(Json) Json (Json) Json (Json) Json(Json) Json (Json) 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 有效 JSON。如果你分析它, 你会得到这样的物体:

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

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

黑客快乐!

其他回答

虽然有些分析者支持C++式的评论,但我使用的是JsonCpp 专案在这些例子中,有这样一个例子:

// 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 (jsonlint)无法验证这一点。 因此, 批注是解析器特定的扩展名, 而不是标准扩展名 。

另一个解析器是杰森5.

JSON 替代 JSON 的替代方案汤加.

另一种替代办法是:jjsonc 喷气管.

最新版本的最新版本nlohmann/json(日元)可选择支持忽略对解析的评论 。

是的,你可以,但你的分析 可能会失败(没有标准)

要分析这些注释,您应该删除这些注释,或者通过手写,或者使用正则表达式:

它取代任何评论,例如:

/****
 * Hey
 */

/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/

它取代任何评论,例如:

// Hey

/\/\/.*/

在JavaScript, 你可以做这样的事情:

jsonString = jsonString.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/, "").replace(/\/\/.*/,"")
var object = JSON.parse(jsonString);

JSON和JSON需要提出评论。至少可在.NET Core 中提供评论。JSON和牛顿软JSON 工作很顺利

{
  // this is a comment for those who is ok with being different
  "regular-json": "stuff"...
}

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 进行评论。 要阅读 JavaScript 的JSON 注释文件, 您可以在解析前删除评论( 参见下面的代码 ) 。 我相信这个代码可以改进, 但对于使用常规表达式的人来说很容易理解 。

我用评论的JSON文件来为我的合成反射系统指定神经元形状。我还用评论的JSON来储存一个运行中的神经元系统的中间状态。非常方便有评论。不要听那些告诉你他们是个坏主意的动作。

fetch(filename).then(function(response) {
    return response.text();
}).then(function(commented) {
    return commented.
        replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '$1').
        replace(/\r/,"\n").
        replace(/\n[\n]+/,"\n");
}).then(function(clean) {
    return JSON.parse(clean);
}).then(function(json) {
    // Do what you want with the JSON object.
});