我可以在json文件里使用注释吗? 如果是的话,怎么写?
当前回答
在json中不允许使用表格///...或/*...* /的注释。
https://www.json.org rfc 4627: Javacampn 对象标记(json)申请/json媒体类型(json) rfc 8259; json) 数据交换格式(supercedes rfcs 4627, 7158, 7159)
其他回答
我真的很喜欢 @eli ' s 方法, 有超过 30 个答案, 但没有人提到列表( array) 。 所以使用 @eli ' s 方法, 我们可以做类似的事情:
"part_of_speech": {
"__comment": [
"@param {String} type - the following types can be used: ",
"NOUN, VERB, ADVERB, ADJECTIVE, PRONOUN, PREPOSITION",
"CONJUNCTION, INTERJECTION, NUMERAL, PARTICLE, PHRASE",
"@param {String} type_free_form - is optional, can be empty string",
"@param {String} description - is optional, can be empty string",
"@param {String} source - is optional, can be empty string"
],
"type": "NOUN",
"type_free_form": "noun",
"description": "",
"source": "https://google.com",
"noun_class": {
"__comment": [
"@param {String} noun_class - the following types can be used: ",
"1_class, 2_class, 3_class, 4_class, 5_class, 6_class"
],
"noun_class": "4_class"
}
}
确定您可以对 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.
});
我搜索了所有页面的答案, 没有人提到关于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在本地没有评论。当然,有时你还是想要评论。对python来说,两种方法就是评论json(和//仅对python 2)或json_tricks(或//对python 2和python 3),这有其他几个特点。免责声明:我做了json_tricks。
否认声明:你的保证无效
正如已经指出的,这个黑客利用了这个规格的实施。并非所有json的采样师都会理解这种json。特别是流的采样师会窒息。
这是一个有趣的好奇, 但你真的不应该使用它 任何东西。下面是最初的答案。
我发现一个小黑客 允许你在一个 Json 文件中发表评论 不影响解析 或改变数据 以任何方式代表。
似乎当声明对象文字时, 您可以用同一个键指定两个值, 而最后一个值优先 。 信不信由你, 事实证明json 采集器的工作方式是一样的 。 这样我们就可以用它来在源代码json 中创建评论, 这些评论不会出现在解析对象表示中 。
({a: 1, a: 2});
// => Object {a: 2}
Object.keys(JSON.parse('{"a": 1, "a": 2}')).length;
// => 1
如果我们运用这个技术, 你评论的json文件可能看起来是这样的:
{
"api_host" : "The hostname of your API server. You may also specify the port.",
"api_host" : "hodorhodor.com",
"retry_interval" : "The interval in seconds between retrying failed API calls",
"retry_interval" : 10,
"auth_token" : "The authentication token. It is available in your developer dashboard under 'Settings'",
"auth_token" : "5ad0eb93697215bc0d48a7b69aa6fb8b",
"favorite_numbers": "An array containing my all-time favorite numbers",
"favorite_numbers": [19, 13, 53]
}
上面的代码是有效的json。 如果你分析它, 你会得到这样的物体:
{
"api_host": "hodorhodor.com",
"retry_interval": 10,
"auth_token": "5ad0eb93697215bc0d48a7b69aa6fb8b",
"favorite_numbers": [19,13,53]
}
意思是评论没有痕迹 也不会有怪异的副作用
快乐的黑客!
推荐文章
- 查询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 ?