问得最多的JSON相关问题之一是“我们可以创建或使用JSON数据格式的注释吗?”。这个问题的答案有点棘手,我们可以是或否。我们将回答我们是否可以创建和使用JSON在本教程从各个角度的意见。
null
我们无法在JSON中正式创建注释
让我们从坏消息开始。JSON的官方定义或标准不支持注释。作为一个简单的数据格式,标准委员会认为这是不必要的,以数据格式创建评论。
JSON5评论
JSON5是由开发人员创建并在GitHub中发布的非官方标准。使用JSON5可以创建JSON标准的一些扩展。其中之一是JSON5支持的注释。根据JSON5,允许使用单行和多行注释。下面是一个JSON5注释的示例。我们看得出来 //
用于创建单行注释,其中 /*
和 */
用于创建多行注释。
{ //First name is used provide the user first name "firstName":"Ahmet", "lastName":"Baydan", "age":7, "birth":"2012-01-01", /* Address block provides the complete address of the user */ "address":{ "streetAddress":"21 2nd Street", "city":"Ankara", "state":"Ankara", "postalCode":6543 }, "phoneNumbers":[ { "type":"home", "number":"212 555-1234" }, { "type":"fax", "number":"646 555-4567" } ]}
使用 _用于注释的注释数据元素
由于JSON并不正式支持注释,但是开发人员非常需要注释,因此他们创建了其他方法来创建注释。其中一个是使用 _comment
JSON数据中的数据元素,如下所示。
{ "_comment":"First name is used provide the user first name", "firstName":"Ahmet", "lastName":"Baydan", "age":7, "birth":"2012-01-01", "_comment":"Address block provides the complete address of the user", "address":{ "streetAddress":"21 2nd Street", "city":"Ankara", "state":"Ankara", "postalCode":6543 }, "phoneNumbers":[ { "type":"home", "number":"212 555-1234" }, { "type":"fax", "number":"646 555-4567" } ]}

使用 用于注释的注释数据元素
我们也可以直接使用 comment
作为与 _comment
.
{ "comment":"First name is used provide the user first name", "firstName":"Ahmet", "lastName":"Baydan", "age":7, "birth":"2012-01-01", "comment":"Address block provides the complete address of the user", "address":{ "streetAddress":"21 2nd Street", "city":"Ankara", "state":"Ankara", "postalCode":6543 }, "phoneNumbers":[ { "type":"home", "number":"212 555-1234" }, { "type":"fax", "number":"646 555-4567" } ]}

使用/**///c和C++类注释
甚至JSON5也支持单行 //
评论和 /*
*/
普通JSON不支持多行注释。我们可以如下使用它们。
{ // First name is used provide the user first name "firstName":"Ahmet", "lastName":"Baydan", "age":7, "birth":"2012-01-01", // Address block provides the complete address of the user "address":{ "streetAddress":"21 2nd Street", "city":"Ankara", "state":"Ankara", "postalCode":6543 }, "phoneNumbers":[ { "type":"home", "number":"212 555-1234" }, { "type":"fax", "number":"646 555-4567" } ]}
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END