我可以在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 字符串, 将会被某个程序读取, 使用前要删除 c 或 c++ 样式的注释会有多困难 ?
回答:如果你这样做,它将是一个班轮。那么json文件可以用作配置文件。
注释中包括您选择的注释;在解析或传输之前用一个分解符将其删除。
i 刚刚发布json.minify () , 它将评论和空白从json区块中分离出来, 并使它成为有效的json, 可以进行剖析。 所以, 您可以使用它 :
JSON.parse(JSON.minify(my_str));
所以我决定写一篇全面的博客文章, 说明为什么评论在json中有意义。
假设您正在使用 json 来保存配置文件, 您想要对这些文件进行注释。 请继续插入您想要的所有评论。 然后通过 jsmin 进行管道, 然后将它交给您的 Jsson 采集器 。 - Douglas crockford, 2012 。
希望这对不同意Json. minify()的原因的人会有帮助。
确定您可以对 json 进行评论。 从 javascript 读取 json 注释文件, 您可以在解析前删除评论( 参见下面的代码) 。 我确信这个代码可以改进, 但对于使用常规表达式的人来说很容易理解 。
我使用备受评论的json文件来指定我的合成反射系统 的神经元形状。 我也使用备受评论的json 来储存一个运行中的神经元系统的中间状态。 方便地得到评论。 不要听那些告诉你他们是个坏主意的人。
fetch(filename).then(function(response) {
return response.text();
}).then(function(commented) {
return commented.
replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '$1').
replace(/\r/,"\n").
replace(/\n[\n]+/,"\n");
}).then(function(clean) {
return JSON.parse(clean);
}).then(function(json) {
// Do what you want with the JSON object.
});
我们用脱衣配对来做我们的项目。 它支持一些东西,比如:
/*
* Description
*/
{
// rainbows
"unicorn": /* ❤ */ "cake"
}
简单的 npm 安装 -- save 条形引言,用于安装和使用它,例如:
var strip_json_comments = require('strip-json-comments')
var json = '{/*rainbows*/"unicorn":"cake"}';
JSON.parse(strip_json_comments(json));
//=> {unicorn: 'cake'}
叹息。为什么不只添加字段,例如:
{
"note1" : "This demonstrates the provision of annotations within a JSON file",
"field1" : 12,
"field2" : "some text",
"note2" : "Add more annotations as necessary"
}
只要确保你的"笔记本"名字 不与任何真实的字段冲突。
推荐文章
- 查询JSON类型内的数组元素
- 将JSON字符串转换为HashMap
- 将JsonNode转换为POJO
- Json_encode()转义正斜杠
- 如何写一个JSON文件在c# ?
- 在序列化和反序列化期间JSON属性的不同名称
- 为什么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 ?