Can comments be used in JSON?

后端 未结 30 2081
别跟我提以往
别跟我提以往 2020-11-22 02:16

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

30条回答
  •  一向
    一向 (楼主)
    2020-11-22 02:33

    No.

    The JSON is data only, and if you include a comment, then it will be data too.

    You could have a designated data element called "_comment" (or something) that should be ignored by apps that use the JSON data.

    You would probably be better having the comment in the processes that generates/receives the JSON, as they are supposed to know what the JSON data will be in advance, or at least the structure of it.

    But if you decided to:

    {
       "_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"
                }
             }
          }
       }
    }
    

提交回复
热议问题