我能否在以下发言中发表一些意见?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 定义评论格式。

正如许多人所暗示的那样,有一些把戏,例如重复键或特定键。_comment你可以使用它,由你决定

其他回答

这是"你能吗"问题。这里有一个问题,"是" "是"回答。

不,你不应该使用重复的物体成员 将侧导道数据塞进 JSON 编码中。 (见“ 对象中的名称应该是独特的 ” )二、《公约》在《公约》中).

是的,你可以插入注释周围杰逊你可以分析出来

但如果你想用一种方式 插入和提取任意的侧通道数据到一个有效的 JSON , 答案是这里。 我们利用 JSON 编码中的数据非独有的表达方式。 这允许 。*在RFC第二节“允许在六个结构字符中的任何字符之前或之后使用白色空间”。

*RFC 仅声明“ 允许在六个结构字符之前或之后使用白色空间”, 没有明确提及字符串、 数字、 “ false ”、“ true ” 和“ null ” 。 在所有执行中忽略了这一遗漏 。


首先,用简单化的方式 将你的JSON化为金字塔:

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

以下是您的输出 :

$jsonWithComment = $steg . $jsonMin;

我们正在使用strip-json-comments支持我们的项目。 它支持一些东西,比如:

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

简简npm install --save strip-json-comments安装和使用它如下:

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

您可以使用JSON-LD (JSON-LD)schema.org 注释用于正确写入备注:

{
    "https://schema.org/comment": "this is a comment"
}

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

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

它取代任何评论,例如:

/****
 * Hey
 */

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

它取代任何评论,例如:

// Hey

/\/\/.*/

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

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

您可以通过正则表达式使用简单的预处理。例如,以下函数将解码在 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风格的评论: / *, #, / /。 字符串的字句保留不变 。