我可以在json文件里使用注释吗? 如果是的话,怎么写?
当前回答
你可以在jsonp中发表评论,但不能在纯json中发表评论。 我只是花了一个小时试图用这个高雅的图表来做我的节目。
如果你遵循链接,你将看见
?(/* AAPL historical OHLC data from the Google Finance API */
[
/* May 2006 */
[1147651200000,67.79],
[1147737600000,64.98],
...
[1368057600000,456.77],
[1368144000000,452.97]
]);
我本地文件夹里也有类似的文件, 与同一来源的政策没有问题, 所以我决定使用纯json... 当然,$. Getjson因为评论而沉默不语。
我最后向上述地址发送了一份手册 http 请求, 并意识到内容类型是文本/ javascript, 因为, jsonp 返回纯正的 javascript 。 在此情况下, 批注是允许的 。 但我的应用程序返回了内容类型应用程序/ json, 所以我不得不删除批注 。
其他回答
叹息。为什么不只添加字段,例如:
{
"note1" : "This demonstrates the provision of annotations within a JSON file",
"field1" : 12,
"field2" : "some text",
"note2" : "Add more annotations as necessary"
}
只要确保你的"笔记本"名字 不与任何真实的字段冲突。
如果您正在使用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 规格不支持评论, // 或/ * * / 样式。
但一些 Json 剖析图书馆和助手支持它们。
类似 :
json5 视觉演播室 代码评论
在撰写本报告时,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兼容的图书馆,这些图书馆支持评论。
一个突出的例子就是“hashcorp语言”(hcl),它是由那些制造流浪者、包装者、领事和保险库的人共同写的。
推荐文章
- 在ASP中设置Access-Control-Allow-OriginNet MVC -最简单的方法
- JWT vs cookie用于基于令牌的身份验证
- JQ:选择多个条件
- 什么是HTTP中的“406-不可接受的响应”?
- 有没有办法注释掉.ASPX页面中的标记?
- ASP。NET Core返回带有状态码的JSON
- 我如何添加环境变量启动。VSCode中的json
- PostgreSQL返回JSON数组的结果集?
- HTTP POST在Java中使用JSON
- 如何指定杰克逊只使用字段-最好是全局
- jQuery为ajax请求返回“parsererror”
- 检查Postgres JSON数组是否包含字符串
- 将bash变量传递给jq
- 在$.ajax()中将数组传递给ajax请求
- 转换JSON字符串到c#对象