我可以在json文件里使用注释吗? 如果是的话,怎么写?
当前回答
纯粹的答案是否定的。
但有些编辑和平台使用变通办法来补充json的评论。
1. 今天,大多数编辑都有内置选项和扩展选项,以对json文件添加评论。 (例如:- vs code 也有一个带有评论(jsonc) 模式/ vs code 的json 也有一个漂亮的扩展)
在 vscode 中激活 jsonc 模式的链接
2. 有些平台提供内置方式来补充评论(简洁json)。 (例如:-在消防基地,我可以对消防基地.jsons发表一段没有问题的评论。)
{
"hosting": {
"headers": [
/*{
"source": "*.html",
"headers": [
{
"key": "Content-Security-Policy",
"value": "default-src 'self' ..."
}
]
},*/
]
}
}
3. 在你自己的json 解析方法中,您可以设置一个预定义的密钥名作为注释。
包括:-
{
"comment" : "This is a comment",
"//" : "This also comment",
"name" : "This is a real value"
}
其他回答
如果您正在使用带有 asp. net 的牛顿软性.json 库来读取/删除您可以在 json 内容中使用批注 :
//"名字": "字符串"//"id":
或
* 这是评论示例 */
ps:单行评论仅用6+版本的牛tonoftjson支持。
给无法从框中思考的人附加注释 : 我使用 json 格式用于 Asp. net 应用程序的基本设置 。 i 读取文件, 将其转换成设置对象 , 并使用 tonoft 库 , 必要时使用 。
我更喜欢在json档案中写关于每个设置的评论, 我真的不在乎json格式的完整性, 只要我使用的图书馆可以使用。
i 认为这比创建单独的“ 设置 ” 设置更容易使用/ 理解 。 读取我的文件, 并解释文件中的设置 。
如果你对这种用法有问题, 对不起, 精灵已经出灯了。 人们会发现 Json 格式的其他用法, 您对此无能为力 。
我搜索了所有页面的答案, 没有人提到关于json语法 突出的Github或堆叠溢出, 尽管这个答案已经接近了。
一些 json 剖析器接受 c++- 风格的评论。 在 guthub 或堆叠溢出上写标记时触发它们。 例如, 您可以指定语法加亮类型为 jsonc 。 例如 :
这一点:
```jsonc
// C++-style comment here
{
"*.md": {
"softwrap": true
},
"colorcolumn": 80,
"savecursor": true,
"scrollbar": true,
"scrollspeed": 5,
"softwrap": true,
"wordwrap": true
}
```
生产以下物品:
// C++-style comment here
{
"*.md": {
"softwrap": true
},
"colorcolumn": 80,
"savecursor": true,
"scrollbar": true,
"scrollspeed": 5,
"softwrap": true,
"wordwrap": true
}
作为上文提及的答案,您可以使用惊人的 nlohmann json c++ 库, 分析有 c (/ * * * /) 和 c++ (/) 样式注释的json 。
https://github.com/nlohmann/json https://github.com/nlohmann/json https://github.com/nlohmann/json.
您需要的单页眉文件在此 : https://github.com/nlohmann/json/blob/development/including/nlohmann/json.hpp
图书馆说: https://github. com/nlohmann/jsoncomments-in-json:
json 此库不支持默认的批注 。 它之所以如此, 有三个原因 : 批注不是json 规格的一部分 。 您可以争辩说 / 或 * / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 参数, 您可以通过 参数忽略/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 参数, 参数, 。 。 您可以值 参数忽略参数, 您可以值 。 /\\\\\\\\\\\\\\\\\\\\ 。 参数 。 。 参数值 。 。 。 。 。 参数可以忽略 。 。 。 。 。 。 。 参数将 。 。 。 。 。 参数将 参数将 。 。 。 。 。 。 参数将 注释将 。 。 。 。 。 。 注释将 注释将 。 。 。 。 。 。 。 。 。 。 。 。 注释将 注释将 注释将 注释将 注释将 。 。 注释将 。
json:::parse () 函数。 见 https://json.nlohmann.me/api/basic_json/parse/
参考文献:
https://gist.github.com/damianedwards/31d245773304ca73556?permarink_comment_id=3574928gistcomment-3574928
至少这是我在Json. org的一眼 所经历的
json的语法在那一页上被视觉化了。
如果您正在使用php, 您可以使用此函数搜索并删除 / / / * 键入 json 字符串中的批注, 然后将其解析成对象/ 数组 :
function json_clean_decode($json, $assoc = true, $depth = 512, $options = 0) {
// search and remove comments like /* */ and //
$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);
if(version_compare(phpversion(), '5.4.0', '>=')) {
$json = json_decode($json, $assoc, $depth, $options);
}
elseif(version_compare(phpversion(), '5.3.0', '>=')) {
$json = json_decode($json, $assoc, $depth);
}
else {
$json = json_decode($json, $assoc);
}
return $json;
}
希望这有帮助!
它取决于您的 json 库。json.net 支持 javascamps 风格的评论,/ * 逗号 * / 。
见另一个堆叠溢出的问题。
推荐文章
- 在VS Code中禁用“Comments are not allowed In JSON”错误
- 如何在Kotlin解析JSON ?
- JSON文件的蒙古导入
- 如何用jQuery / JavaScript解析JSON数据?
- jQuery在请求体中发布有效的json
- 将JSON显示为HTML
- Swift:理解// MARK
- 在JSON键名中哪些字符是有效的/无效的?
- 如何JSON序列化集?
- 如何检查JSON键是否存在?
- 在构建中编写注释的语法是什么?gradle文件?
- 将对象序列化为JSON
- node.js TypeError:路径必须是绝对路径或指定根路径到res.sendFile[解析JSON失败]
- Python json。loads显示ValueError:额外数据
- JSON省略了Infinity和NaN;JSON在ECMAScript中的状态?