【发布时间】:2020-03-02 23:39:00
【问题描述】:
我需要为直接在根对象中以数组形式出现的数据创建一个 JSON 模式,未命名。这种 JSON 的 MWE 将是:
{
[
{
"veggieName": "potato",
"veggieLike": true
},
{
"veggieName": "broccoli",
"veggieLike": false
}
]
}
我已经看到了验证这样一个未嵌套在对象中的数组的模式示例。我还看到了在命名数组时起作用的示例,例如
{
vegetables : [
{
"veggieName": "potato",
"veggieLike": true
},
{
"veggieName": "broccoli",
"veggieLike": false
}
]
}
第二个例子可以通过模式验证
{
"$id": "https://example.com/arrays.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "A representation of a person, company, organization, or place",
"type": "object",
"properties": {
"vegetables": {
"type": "array",
"items": { "$ref": "#/definitions/veggie" }
}
},
"definitions": {
"veggie": {
"type": "object",
"required": [ "veggieName", "veggieLike" ],
"properties": {
"veggieName": {
"type": "string",
"description": "The name of the vegetable."
},
"veggieLike": {
"type": "boolean",
"description": "Do I like this vegetable?"
}
}
}
}
}
但问题是,一旦删除了“vegetables”这个名称,我就无法找到定义有效模式的方法。如何在模式中正确表示我的数据结构?
(MWE 源自 http://json-schema.org/learn/miscellaneous-examples.html)。
【问题讨论】:
-
基于标题而非内容的副本。其他问题表明他们使用了一个定义根元素必须是数组的模式,这个问题不是。
-
好点@Relequestual 我撤回了接近投票。
标签: arrays json jsonschema