我可以在json文件里使用注释吗? 如果是的话,怎么写?
当前回答
在json中不允许使用表格///...或/*...* /的注释。
https://www.json.org rfc 4627: Javacampn 对象标记(json)申请/json媒体类型(json) rfc 8259; json) 数据交换格式(supercedes rfcs 4627, 7158, 7159)
其他回答
否认声明:你的保证无效
正如已经指出的,这个黑客利用了这个规格的实施。并非所有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 编码 。 (请参见 rfc 中“ 对象中的名称应该是独一无二的 ” ) 。
是的,你可以在json周围插入评论, 你可以分析出来。
但如果你想将任意的侧道数据插入和提取到一个有效的json 中, 请使用一个答案 。 我们利用 Json 编码中的数据非独有的表达方式。 在 rfc 的第二节中, “ 允许在六个结构字符中的任何字符之前或之后使用白色空间 ” 。
* rfc 仅表示“在六个结构字符之前或之后允许有白色空间”,但没有明确提及字符串、数字、“假”、“真”和“null”。 在所有执行中,这一遗漏被忽略。
首先,将你的json拼写成拼写:
$jsonMin = json_encode(json_decode($json));
然后将您的评论编码为二进制 :
$hex = unpack('H*', $comment);
$commentBinary = base_convert($hex[1], 16, 2);
然后输入您的二进制 :
$steg = str_replace('0', ' ', $commentBinary);
$steg = str_replace('1', "\t", $steg);
您的输出 :
$jsonWithComment = $steg . $jsonMin;
在我目前的项目中,我遇到了这个问题, 因为我有相当多的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
}
"""))
这是我在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"
}
注释不是官方标准,尽管有些分析者支持 c++- 风格的注释。
// Configuration options
{
// Default encoding for text
"encoding" : "UTF-8",
// Plug-ins loaded at start-up
"plug-ins" : [
"python",
"c++",
"ruby"
],
// Tab indent size
"indent" : { "length" : 3, "use_space": true }
}
jsonlint 不验证这一点 。 因此, 批注是解析器特定的扩展名, 而不是标准 。
另一本书是Json5。
取代json Toml的替代品。
另一种选择是jsonc。
Nlohmann/json的最新版本有选择的支持,
推荐文章
- 在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#对象