我能否在以下发言中发表一些意见?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如果有,如何处理?


当前回答

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

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

它取代任何评论,例如:

/****
 * Hey
 */

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

它取代任何评论,例如:

// Hey

/\/\/.*/

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

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

其他回答

写作时缩略语.json支持意见。

例如(样本样本样本瑞微软)

{
  "Logging": {
    "LogLevel": { // All providers, LogLevel applies to all the enabled providers.
      "Default": "Error", // Default logging, Error and higher.
      "Microsoft": "Warning" // All Microsoft* categories, Warning and higher.
    },
    "Debug": { // Debug provider.
      "LogLevel": {
        "Default": "Information", // Overrides preceding LogLevel:Default setting.
        "Microsoft.Hosting": "Trace" // Debug:Microsoft.Hosting category.
      }
    },
    "EventSource": { // EventSource provider
      "LogLevel": {
        "Default": "Warning" // All categories of EventSource provider.
      }
    }
  }
}

您可以通过正则表达式使用简单的预处理。例如,以下函数将解码在 PHP 中评论 JSON:

function json_decode_commented ($data, $objectsAsArrays = false, $maxDepth = 512, $opts = 0) {
  $data = preg_replace('~
    (" (?:[^"\\\\] | \\\\\\\\ | \\\\")*+ ") | \# [^\v]*+ | // [^\v]*+ | /\* .*? \*/
  ~xs', '$1', $data);

  return json_decode($data, $objectsAsArrays, $maxDepth, $opts);
}

它支持所有PHP风格的评论: / *, #, / /。 字符串的字句保留不变 。

这是我在Google 火底文件允许你对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"
}

你,你,你能够有评论意见的年份贾索普我只是花了一个小时 试图让程序与例如,Highcharts的这个例子.

如果你跟着链接,你会看到

?(/* AAPL historical OHLC data from the Google Finance API */
[
/* May 2006 */
[1147651200000,67.79],
[1147737600000,64.98],
...
[1368057600000,456.77],
[1368144000000,452.97]
]);

因为我在本地文件夹里也有类似的文件 没有问题同源政策所以我决定使用纯JSON... 当然,$.getJSON由于这些评论而无声无息地失败。

最后,我刚刚向上述地址发送了 HTTP 手动请求, 并意识到内容类型是text/javascript自此, JSONP 返回纯 JavaScript 。允许但我的应用程序返回了内容类型application/json所以我不得不去掉那些评论

纯粹的答案是否 无.

但有些编辑和平台利用变通办法向JSON提供评论。

1今天,大多数编辑都有内在的选择和扩展,以补充对JSON文件的评论。 (例如: - VS 代码还有一个带有评论的JSON。(jsonc) mode《VS法典》也有很好的展期)

VsCode 中激活 jsonc 模式的链接

2一些平台为补充评论(简简json)提供了内在途径。 (例如:- 在消防基地,我可以评论firebase.json暂时没有问题。

    {
    "hosting": {
        "headers": [
            /*{
              "source": "*.html",
              "headers": [
                {
                  "key": "Content-Security-Policy",
                  "value": "default-src 'self' ..."
                }
              ]
            },*/
        ]
      }
    }

3。在您自己的 JSON 解析方法中,您可以设置一个预定义的密钥名称作为注释。

包括:-

     {
        "comment" : "This is a comment",
        "//" :  "This also comment",
        "name" : "This is a real value"
     }