Can comments be used in JSON?

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

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

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

    If you are using the Newtonsoft.Json library with ASP.NET to read/deserialize you can use comments in the JSON content:

    //"name": "string"

    //"id": int

    or

    /* This is a

    comment example */

    PS: Single-line comments are only supported with 6+ versions of Newtonsoft Json.

    Additional note for people who can't think out of the box: I use the JSON format for basic settings in an ASP.NET web application I made. I read the file, convert it into the settings object with the Newtonsoft library and use it when necessary.

    I prefer writing comments about each individual setting in the JSON file itself, and I really don't care about the integrity of the JSON format as long as the library I use is OK with it.

    I think this is an 'easier to use/understand' way than creating a separate 'settings.README' file and explaining the settings in it.

    If you have a problem with this kind of usage; sorry, the genie is out of the lamp. People would find other usages for JSON format, and there is nothing you can do about it.

提交回复
热议问题