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


当前回答

这是一个"你能"的问题。 这是"可以"的答案。

否, 您不应该使用重复对象成员将侧通道数据塞进json 编码 。 (请参见 rfc 中“ 对象中的名称应该是独一无二的 ” ) 。

是的,你可以在json周围插入评论, 你可以分析出来。

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

* rfc 仅表示“在六个结构字符之前或之后允许有白色空间”,但没有明确提及字符串、数字、“假”、“真”和“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;

其他回答

正如杰森发明家所说:

json 没有评论。 json 编码器不能输出评论。 json 编码器可以接受和忽略评论。

工具 jq 包括一个解码器,它允许“ ” 式的注释,因此jq 是可与 json- with- comments 文件一起使用的几种工具之一, 只要这些文件被当作“ jq 程序” 处理, 而不是作为 json 文件处理 。 例如 :

$ jq -ncf <(echo $'[1, # one\n2 ] # two') 
[1,2]

更重要的是,jq可以作为程序处理非常大的json-with comments文件;这可以用一个众所周知的json文件来说明:

$ ls -l JEOPARDY_QUESTIONS1.json
-rw-r--r--  2 xyzzy  staff  55554625 May 12  2016 JEOPARDY_QUESTIONS1.json

$ jq -nf JEOPARDY_QUESTIONS1.json | jq length
216930

在我目前的项目中,我遇到了这个问题, 因为我有相当多的json 需要一些评论 使事情容易记住。

i've used this simple python 函数来替换批注并使用 json. loads 将其转换成 dict:

import json, re

def parse_json(data_string):
  result = []
  for line in data_string.split("\n"):
    line = line.strip()
    if len(line) < 1 or line[0:2] == "//":
      continue
    if line[-1] not in "\,\"\'":
      line = re.sub("\/\/.*?$", "", line)
    result.append(line)
  return json.loads("\n".join(result))

print(parse_json("""
{
  // This is a comment
  "name": "value" // so is this
  // "name": "value"
  // the above line gets removed
}
"""))

至少这是我在Json. org的一眼 所经历的

json的语法在那一页上被视觉化了。

没有。

json is only data. 如果您包含一个注释, 那么它也必须是数据 。

您可以有一个名为“_comment”(或某物)的指定数据元素,该元素应该被使用json数据的应用程序忽略。

您也许最好在生成/接收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"
            }
         }
      }
   }
}

注释不是官方标准,尽管有些分析者支持 c++- 风格的注释。

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

另一本书是Json5。

取代json Toml的替代品。

另一种选择是jsonc。

Nlohmann/json的最新版本有选择的支持,