天天看点

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不支持注释。

“I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability,” writes Douglas Crockford, 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
}
           

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

:

现在,我们将从文件

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)

JSMin is another option to consider.

JSMin是要考虑的另一种选择。

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 (amymhaddad.com).

我写了有关学习编程和实现它的最佳方法的文章( amymhaddad.com )。

翻译自: https://www.freecodecamp.org/news/json-comment-example-how-to-comment-in-json-files/

json文件中注释