根据JSON API规范网站上的建议,我们应该使用JSON中用连字符分隔的所有小写成员名称:
URL安全命名的允许和推荐字符 成员以规范格式定义。还要标准化成员 名称,建议使用以下(更严格的)规则:
会员名称应该以字符“a-z”开头和结尾(U + 0061到 U + 007A)会员名称应该只包含字符“a-z”(U + 0061 至U + 007A),“0-9”(U + 0030至U + 0039),连字符减(U + 002D) HYPHEN-MINUS,“ - ”)作为多个单词之间的分隔符。
基本上,我们应该像这样使用JSON:
{
"first-name": "Jason",
"last-name": "Tough"
}
它不会使cumbersome访问JavaScript中的这些属性吗?或者任何其他编程语言,特别是如果我们想从JSON Schema生成类?
这项建议背后的动机是什么?
答案 0 :(得分:2)
我对此有同样的疑问,我发现这个解释为什么我不应该使用 namingDirectives 的下划线。它不一样,但看起来非常相似:
The UNDERSCORE character ("_") may be used in filenames and directory names where an application (unavoidably) generates this character, but in general, use of HYPHEN to mark juncture is preferable; the UNDERSCORE character may be visually confused with SPACE or an underline-effect in some predictable publication contexts. An UNDERSCORE must never be used in a filename or directory name that is used in a document URI — that is, a primary URI reference published as a document cover page URI (i.e. as required for identification of a Work Product as a whole or for identification of a separately-titled prose Part in a Multi-Part Work Product).
答案 1 :(得分:0)
看起来像从引用中引用的JSONAPI不再有效。当前是“ JSONAPI recommends:
命名
该规范对成员的方式设置了一些严格的限制 (即密钥)在JSON:API文档中的名称。进一步规范 成员名称,这在混合配置文件时尤其重要 由不同方创作,以下规则也 推荐:
Member names SHOULD be camel-cased (i.e., wordWordWord) Member names SHOULD start and end with a character “a-z” (U+0061 to U+007A) Member names SHOULD contain only ASCII alphanumeric characters (i.e., “a-z”, “A-Z”, and “0-9”)
所以上面的示例应该是:
{
"firstName": "Jason",
"lastName": "Tough"
}