Can comments be used in JSON?

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

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

30条回答
  •  生来不讨喜
    2020-11-22 02:28

    Comments are not an official standard, although some parsers support C++-style comments. One that I use is JsonCpp. In the examples there is this one:

    // 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 does not validate this. So comments are a parser specific extension and not standard.

    Another parser is JSON5.

    An alternative to JSON TOML.

    A further alternative is jsonc.

    The latest version of nlohmann/json has optional support for ignoring comments on parsing.

提交回复
热议问题