我可以在json文件里使用注释吗? 如果是的话,怎么写?
当前回答
确定您可以对 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.
});
其他回答
因此我将调试信息放入 http 网站页眉, 以避免打破客户端:
header("My-Json-Comment: Yes, I know it's a workaround ;-) ");
调
如果您以文本文件的形式装入, 您可以使用带有注释的 json , 然后删除注释 。
例如,您可以为此使用 decomment 库。下面是一个完整的示例。
输入json (文件输入.js) :
/*
* multi-line comments
**/
{
"value": 123 // one-line comment
}
测试应用程序 :
var decomment = require('decomment');
var fs = require('fs');
fs.readFile('input.js', 'utf8', function (err, data) {
if (err) {
console.log(err);
} else {
var text = decomment(data); // removing comments
var json = JSON.parse(text); // parsing JSON
console.log(json);
}
});
输出 :
{ value: 123 }
ưμ㼯A
这是我在Google Firebbase文档中找到的东西, 可以让你在json中发表评论:
{
"//": "Some browsers will use this to enable push notifications.",
"//": "It is the same for all projects, this is not your project's sender ID",
"gcm_sender_id": "1234567890"
}
如果您的上下文是节点. js 配置, 您可以考虑通过模块使用 javascript 。 exports 作为json 的替代 :
module.exports = {
"key": "value",
// And with comments!
"key2": "value2"
};
需要的语法将保持不变。 作为 javascript, 文件扩展名应该是 .js 。
如果您的文本文件, 即json 字符串, 将会被某个程序读取, 使用前要删除 c 或 c++ 样式的注释会有多困难 ?
回答:如果你这样做,它将是一个班轮。那么json文件可以用作配置文件。
推荐文章
- 查询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 ?