我可以在json文件里使用注释吗? 如果是的话,怎么写?
当前回答
json speces 并不支持评论, 但是您可以将评论写成密钥来解决这个问题, 比如这样 。
{
"// my own comment goes here":"",
"key1":"value 1",
"// another comment goes here":"",
"key 2": "value 2 here"
}
我们使用评论文本作为关键, 以确保它们( 几乎) 是独一无二的, 它们不会打破任何解析器。 如果有些评论不是独一无二的, 请在结尾处添加随机数字 。
如果您需要分析任何处理中的评论, 比如剥除这些评论, 您可以用文字填充注释值, 表示它是一个注释 , 比如 :
{
"// my own comment goes here" : "_comment",
"key1":"value 1",
"// another comment goes here" : "_comment",
"key 2": "value 2 here"
}
以此方式, 分析者可以找到所有的评论并处理它们 。
其他回答
在撰写本报告时,Appings.json支持评论。
例如(微软的样本)
{
"Logging": {
"LogLevel": { // All providers, LogLevel applies to all the enabled providers.
"Default": "Error", // Default logging, Error and higher.
"Microsoft": "Warning" // All Microsoft* categories, Warning and higher.
},
"Debug": { // Debug provider.
"LogLevel": {
"Default": "Information", // Overrides preceding LogLevel:Default setting.
"Microsoft.Hosting": "Trace" // Debug:Microsoft.Hosting category.
}
},
"EventSource": { // EventSource provider
"LogLevel": {
"Default": "Warning" // All categories of EventSource provider.
}
}
}
}
在我目前的项目中,我遇到了这个问题, 因为我有相当多的json 需要一些评论 使事情容易记住。
i've used this simple python 函数来替换批注并使用 json. loads 将其转换成 dict:
import json, re
def parse_json(data_string):
result = []
for line in data_string.split("\n"):
line = line.strip()
if len(line) < 1 or line[0:2] == "//":
continue
if line[-1] not in "\,\"\'":
line = re.sub("\/\/.*?$", "", line)
result.append(line)
return json.loads("\n".join(result))
print(parse_json("""
{
// This is a comment
"name": "value" // so is this
// "name": "value"
// the above line gets removed
}
"""))
如果您正在使用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兼容的图书馆,这些图书馆支持评论。
一个突出的例子就是“hashcorp语言”(hcl),它是由那些制造流浪者、包装者、领事和保险库的人共同写的。
注释中包括您选择的注释;在解析或传输之前用一个分解符将其删除。
i 刚刚发布json.minify () , 它将评论和空白从json区块中分离出来, 并使它成为有效的json, 可以进行剖析。 所以, 您可以使用它 :
JSON.parse(JSON.minify(my_str));
所以我决定写一篇全面的博客文章, 说明为什么评论在json中有意义。
假设您正在使用 json 来保存配置文件, 您想要对这些文件进行注释。 请继续插入您想要的所有评论。 然后通过 jsmin 进行管道, 然后将它交给您的 Jsson 采集器 。 - Douglas crockford, 2012 。
希望这对不同意Json. minify()的原因的人会有帮助。
推荐文章
- 在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中的状态?