javascript 添加json文件注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11195101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add json file comments
提问by Ash Blue
Possible Duplicate:
Can I comment a JSON file?
可能重复:
我可以评论 JSON 文件吗?
We are using a .json
file on a project. We want to know if we can add comments to the file and avoid crashing the JSON parser.
我们正在.json
项目中使用文件。我们想知道是否可以向文件添加注释并避免 JSON 解析器崩溃。
We've tried to do so with the following comment types, but they all crash the JSON file when it's parsed:
我们尝试使用以下注释类型来这样做,但它们在解析 JSON 文件时都会崩溃:
# I crash
// I crash
/* I crash */
Is there an acceptable form of commenting for JSON files?
是否有可接受的 JSON 文件注释形式?
采纳答案by Rich Bradshaw
JSON doesn't support comments – which is good when you think about it. However, someone has made JSON5 (https://github.com/aseemk/json5), which does, and may be of use to you.
JSON 不支持注释——当你考虑它时,这很好。但是,有人制作了 JSON5 ( https://github.com/aseemk/json5),它确实可以,并且可能对您有用。
It's worth pointing out that this is just someones JSON-like project, and is not an official spec, but then I guess JSON is just someones XML-like project that people liked :)
值得指出的是,这只是某人的类似 JSON 的项目,而不是官方规范,但我猜 JSON 只是人们喜欢的某人的类似 XML 的项目:)
回答by tskuzzy
The standard JSON format doesn't explicitly support file comments. RFC 4627 application/json
标准 JSON 格式不明确支持文件注释。RFC 4627 应用程序/json
It's a lightweight format for storing and transferring data. If the comment is truly important, you can include it as another data field like comments: "my comment"
.
它是一种用于存储和传输数据的轻量级格式。如果评论真的很重要,您可以将其作为另一个数据字段包含在内,如comments: "my comment"
.
e.g.
例如
{
name: "Bob",
age: 5,
comments: "I don't like him"
}
However if it's being used in this format, it really is just another piece of data. So ultimately, what you have to realize is that just because certain fields are there doesn't mean you have to use it.
但是,如果以这种格式使用它,它实际上只是另一条数据。所以最终,您必须意识到,仅仅因为存在某些字段并不意味着您必须使用它。