Can comments be used in JSON?

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

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

30条回答
  •  暖寄归人
    2020-11-22 02:39

    If you are using Jackson as your JSON parser then this is how you enable it to allow comments:

    ObjectMapper mapper = new ObjectMapper().configure(Feature.ALLOW_COMMENTS, true);
    

    Then you can have comments like this:

    {
      key: "value" // Comment
    }
    

    And you can also have comments starting with # by setting:

    mapper.configure(Feature.ALLOW_YAML_COMMENTS, true);
    

    But in general (as answered before) the specification does not allow comments.

提交回复
热议问题