宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

Jon文件中的注释

if you’rehavingtroubleaddingcommentstoyourjsonfile,there’sagoodreason : JSON doesn’tsupportcomments。

如果向JSON文件添加注释时出现问题,则有充分理由认为JSON不支持注释。

” iremovedcommentsfromjsonbecauseisawpeoplewereusingthemtoholdparsingdirectives,apracticewhichwouldhavedestroyedinteroperoperabilabives

“从JSON中删除了评论。 因为我看到人们用它们保持着分析命令。 ssdxh写道:“这种做法会损害互操作性。”

However,there’sa workaround.and that’swhatthisarticleisabout : howtoaddcommentstoyourjsonfile。

但是,有一个解决方法。 这就是本文的主题。 如何将注释添加到JSON文件。

将数据作为注释添加(Add Data as Comments ) awaytoskirtaroundthecommentsissueistoaddatatoyourjsonfilethatfunctionascomments。

解决注释问题的一种方法是将数据作为注释添加到JSON文件中。

let’sgothroughanexample,startingwiththisinformationinourjsonfile :

让我们看一个例子。 让我们从JSON文件的以下信息开始。

{ ‘sport’: ‘basketball ‘,’ coach’: ‘Joe Smith ‘,’ wins’: 15,’ losses ‘ : ‘ now let ‘ saddanotherkkkket }

现在,让我们添加另一个键值对作为注释。 显示在以下代码的第一行。

{ ‘ _ comment1’ : ‘ thisismycomment ‘,’ sport’: ‘basketball ‘,’ coach’: ‘Joe Smith ‘,’ wins ‘ 3360 15

这是另一个例子。 这次,在钥匙的开头和结尾使用两条下划线。

‘ _ _ comment2_ ‘ : ‘ thisisanothercomment ‘,theunderscoreshelptodifferentiatethecommentfromtherestofthedatainour

下划线有助于将注释与文件中的其他数据区分开。

警告(there’sanimportantdetailtokeepinmind )。

有需要记住的重要细节。

thecommentsweaddedtoourjsonfileareincludedinthejsonobject.inotherwords,the comments are treated as data。

我们添加到JSON文件的注释包含在JSON对象中。 也就是说,注释被视为数据。

英雄’英雄。

这就是我们的意思。

This is the code in our file,data.json:

这是我们的文件data.json的代码:

{ ‘ _ comment1’ : ‘ thisismycomment ‘,’ sport’: ‘basketball ‘,’ coach’: ‘Joe Smith ‘,’ wins ‘ 3360 15

现在从文件 read_comments.py读取此数据。

importJSONwithopen(data.JSON ),mode=’r ‘ ) asj_object:data=JSON.load ) j_object ) print ) data ) there re

结果中包含了我们的评论:

{ ‘ _ comment1’ : ‘ thisismycomment ‘,’ sport’: ‘basketball ‘,’ coach’: ‘Joe Smith ‘,’ wins ‘ 3360 15

也可以从JSON对象中提取注释值。 this is my comment :

importJSONwithopen(data.JSON ),mode=’ r ‘ (asj _ object : data=JSON.load (j _ object ) ) print (data (‘ _ CT )

请记住,评论是开发者眼中的评论,而不是计算机。

不同类型的注释(A Different Type of Comment ) thisjsoncommentingpracticeisdifferentfromcommentsinprogramminglanguages,like Python

与Python等编程语言中的注释不同,此JSON注释的做法通常在程序运行时被忽略。

# here ‘ smycommentword=’ house ‘ forletterinword : print (letter ) When we run the Python program above,wegetthelettersinthe

在此基础上运行Python程序,可以得到单词“house”的文字。 但是我们没有看评论。 会被忽视。

注释选项JSMin is another option to consider。

JSMin是需要考虑的另一个选择。

it’satoolthatremovesextrawhitespaceandcommentsfromjavascriptfiles.butitalsoworksonjsonfiles.jsminremovescommentsfromjjson

这是一个从JavaScript文件中删除多余空格和注释的工具。 但是,这也适用于JSON文件。 JSMin会在分析之前从JSON文件中删除注释。

sothereareoptionswhenitcomestocommentinginjsonfiles.although they ‘ renotperfectsolutions,atleastherearewaystoincludethedord

因此,在JSON文件中添加注释时有几个选项。 虽然这些都不是完美的解决方案,但至少有几种方法可以根据需要包含所需的文档。

iwriteaboutlearningtoprogramandthebestwaystogoaboutit (amymhaddad.com)。

学习编程,并写了一篇关于实现它的最佳方法的文章(amymhaddad.com)。

3:https://www.freecode camp.org/news/JSON-comment-example-how-to-comment-in-JSON-files /

Jon文件中的注释