我尝试为我的数据编写一个json模式。数据看起来像这样:
{
"gold": [
{
"id": "goldOne",
"name": "firstGold",
"title": "Gold 1 earned"
},
{
"id": "goldTwo",
"name": "secondGold",
"title": "Gold 2 earned"
}
],
"silver": [
{
"id": "silberOne",
"name": "firstSilver",
"title": "Silver!"
}
],
"bronze": [
{
"id": "bronzeOne",
"name": "firstBronze",
"title": "Bronze!"
}
]
}
我已经为“金”数组创建了架构:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title" : "trophy descriptions",
"type": "object",
"properties": {
gold: {
"description": "gold trophies",
"type":"array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "unique identifier"
},
"name": {
"type": "string",
"description": "label of trophy"
},
"title": {
"type": "string",
"description": "description of trophy"
}
}
}
}
}
}
因为“银”和“青铜”数组包含的元素类型与“金”的类型完全相同,所以我想知道是否必须写下三遍相同的内容,还是只能引用一个描述?
是的,您可以通过$ ref关键字定义和引用架构:
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"title" : "trophy descriptions",
"type" : "object",
"properties" : {
"gold" : {
"$ref" : "#/definitions/medal"
},
"silver" : {
"$ref" : "#/definitions/medal"
},
"bronze" : {
"$ref" : "#/definitions/medal"
}
},
"definitions" : {
"medal" : {
"type" : "array"
// and whatever you want here
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句