我可以在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;
}
希望这有帮助!
其他回答
如果您使用 json5, 您可以包含注释 。
json5建议扩展至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 规格不支持评论, // 或/ * * / 样式。
但一些 Json 剖析图书馆和助手支持它们。
类似 :
json5 视觉演播室 代码评论
json背后的想法是提供各种应用程序之间的简单数据交换。 这些应用程序通常是基于网络的,语言是javascript。
但是,虽然数据显然需要由解析代码忽略或具体处理, 但是,它实际上不允许这样的评论, 但是,作为数据中的名称/价值对之一,通过一个评论肯定行得通,
简逊档案不应该包含传统意义上的评论。 它应该只是数据。
详情请查看json网站。
您可以使用 json-ld 和 schema.org 批注类型来正确写备注:
{
"https://schema.org/comment": "this is a comment"
}
推荐文章
- 为什么PHP的json_encode函数转换UTF-8字符串为十六进制实体?
- Ajax会调用什么样的响应,比如'for (;;);{json data}的意思?
- 在JavaScript中将JSON字符串解析为特定对象原型
- 使用Jackson将JSON字符串转换为漂亮的打印JSON输出
- jQuery。由于转义了JSON中的单引号,parseJSON抛出“无效JSON”错误
- 用c#解析JSON
- 如何合并2 JSON对象从2个文件使用jq?
- 在VS Code中禁用“Comments are not allowed In JSON”错误
- 如何在Kotlin解析JSON ?
- JSON文件的蒙古导入
- 如何用jQuery / JavaScript解析JSON数据?
- jQuery在请求体中发布有效的json
- 将JSON显示为HTML
- Swift:理解// MARK
- 在JSON键名中哪些字符是有效的/无效的?