建立数据库系统的主要目标json⽂件中注释_JSON注释⽰例—如何在JSON⽂件中进⾏注
json⽂件中注释
If you’re having trouble adding comments to your JSON file, there’s a good reason: JSON doesn’t support comments.
如果您在将注释添加到JSON⽂件时遇到问题,则有充分的理由:JSON不⽀持注释。
spring用英语怎么说“I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability,” writes , who popularized the text-based data format.
“我从JSON中删除了注释,因为我看到⼈们使⽤它们保留解析指令,这种做法会破坏互操作性,”普及基于⽂本的数据格式的写道。
However, there’s a workaround. And that’s what this article is about: how to add comments to your JSON file.
但是,有⼀种解决⽅法。 这就是本⽂的主题:如何在JSON⽂件中添加注释。
将数据添加为注释 (Add Data as Comments)
A way to skirt around the comments issue is to add data to your JSON file that function as comments.
⼀种解决注释问题的⽅法是将数据添加到您的JSON⽂件中,以作为注释。
Let’s go through an example, starting with this information in our JSON file:
让我们来看⼀个⽰例,从JSON⽂件中的以下信息开始:
{
"sport": "basketball",
"coach": "Joe Smith",
"wins": 15,
"losses": 5
small翻译
}
数组统一初始化Now let’s add another key-value pair to serve as our comment, which you can see in the first line in the code below:
现在,让我们添加另⼀个键值对作为我们的注释,您可以在下⾯的代码的第⼀⾏中看到它:
{
"_comment1": "this is my comment",
"sport": "basketball",
"coach": "Joe Smith",
"wins": 15,
"losses": 5
海淀云计算培训}
Here’s another example. This time, we use two underscores at the start and end of the key:
这是另⼀个例⼦。 这次,我们在密钥的开头和结尾使⽤两个下划线:
"__comment2__": "this is another comment",
The underscores help to differentiate the comment from the rest of the data in our file.
下划线有助于将注释与⽂件中的其他数据区分开。
告诫 (A Word of Caution)
There’s an important detail to keep in mind.
有⼀个重要的细节要牢记。
The comments we added to our JSON file are included in the JSON object. In other words, the comments are treated as data.
我们添加到JSON⽂件中的注释包含在JSON对象中。 换句话说,注释被视为数据。
Here’s what we mean.
这就是我们的意思。
This is the code in our file, data.json:
这是我们⽂件data.json的代码:
{
"_comment1": "this is my comment",
"sport": "basketball",
"coach": "Joe Smith",
"wins": 15,
"losses": 5
}
Now we’re going to read that data from the file, read_comments.py:
python解析json文件现在,我们将从⽂件read_comments.py读取该数据:
import json
with open("data.json", mode="r") as j_object:
data = json.load(j_object)
print(data)
The result includes our comment:
结果包括我们的评论:
{'_comment1': 'this is my comment', 'sport': 'basketball', 'coach': 'Joe Smith', 'wins': 15, 'losses': 5}
We can even extract the comment's value from the JSON object: this is my comment:
我们甚⾄可以从JSON对象中提取评论的值: this is my comment :
import json
with open("data.json", mode="r") as j_object:
data = json.load(j_object)
print(data["_comment1"])
Keep in mind that the comment is only a comment in the eyes of the developer—not the computer.
请记住,评论只是开发⼈员(⽽不是计算机)眼中的评论。
不同类型的评论 (A Different Type of Comment)
This JSON commenting practice is different from comments in programming languages, like Python, which are typically ignored when the program runs.
这种JSON注释做法与Python等编程语⾔中的注释不同,该注释通常在程序运⾏时被忽略。
# Here's my comment
word = "house"
for letter in word:
print(letter)
When we run the Python program above, we get the letters in the word, “house.” But we don’t see the comment. It’s ignored.
当我们在上⾯运⾏Python程序时,我们得到单词“ house”的字母。 但是我们没有看到评论。 它被忽略。
评论选项 (Commenting Options)
is another option to consider.
是要考虑的另⼀种选择。
It’s a tool that removes extra whitespace and comments from JavaScript files. But it also works on JSON files. JSMin removes comments from JSON files before they're parsed.
这是⼀个从JavaScript⽂件中删除多余空格和注释的⼯具。 但它也适⽤于JSON⽂件。 JSMin在解析之前从JSON⽂件中删除注释。
So there are options when it comes to commenting in JSON files. Although they're not perfect solutions, at least there are ways to include the documentation you need when you need it.
因此,在JSON⽂件中添加注释时有⼀些选项。 尽管它们不是完美的解决⽅案,但⾄少有⼀些⽅法可以在需要时包括所需的⽂档。
I write about learning to program and the best ways to go about it ().
我写了有关学习编程和实现它的最佳⽅法的⽂章( )。
翻译⾃:
json⽂件中注释

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。