我可以在json文件里使用注释吗? 如果是的话,怎么写?
当前回答
json 规格不支持评论, // 或/ * * / 样式。
但一些 Json 剖析图书馆和助手支持它们。
类似 :
json5 视觉演播室 代码评论
其他回答
我刚刚找到了"咕噜咕噜咕噜咕噜声"
“从json那里提取评论, 让你在json档案中使用评论!”
{
// Rainbows
"unicorn": /* ❤ */ "cake"
}
是的,你可以,但你的分析可能会失败(没有标准)。
要进行剖析,您应该删除这些注释,或者通过手写,或者使用正则表达式:
它替换任何评论,例如:
/****
* Hey
*/
/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/
它替换任何评论,例如:
// Hey
/\/\/.*/
你可以做这样的事情:
jsonString = jsonString.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/, "").replace(/\/\/.*/,"")
var object = JSON.parse(jsonString);
我真的很喜欢 @eli ' s 方法, 有超过 30 个答案, 但没有人提到列表( array) 。 所以使用 @eli ' s 方法, 我们可以做类似的事情:
"part_of_speech": {
"__comment": [
"@param {String} type - the following types can be used: ",
"NOUN, VERB, ADVERB, ADJECTIVE, PRONOUN, PREPOSITION",
"CONJUNCTION, INTERJECTION, NUMERAL, PARTICLE, PHRASE",
"@param {String} type_free_form - is optional, can be empty string",
"@param {String} description - is optional, can be empty string",
"@param {String} source - is optional, can be empty string"
],
"type": "NOUN",
"type_free_form": "noun",
"description": "",
"source": "https://google.com",
"noun_class": {
"__comment": [
"@param {String} noun_class - the following types can be used: ",
"1_class, 2_class, 3_class, 4_class, 5_class, 6_class"
],
"noun_class": "4_class"
}
}
Dojo工具包 javascript工具包(至少是1.4版) 允许您在 Json 中加入评论。 评论可以是 / * * / 格式。 Dojo 工具包通过 dojo. xhrget () 电话消耗json 。
其它标本工具箱也可以类似地使用。
在选择最后选择之前先试验其他数据结构(甚至数据清单)时,这样做会有帮助。
至少这是我在Json. org的一眼 所经历的
json的语法在那一页上被视觉化了。
推荐文章
- 在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中的状态?