提前致谢。
我是 JSON 和 JSON 模式的新手。尝试为元组数组生成 JSON 模式。但它不会像所有类似类型的元组的循环一样验证多个记录。 以下是 json 示例。
{
"Data":
[
[ 100, "Test", 2.5 ],
[ 101, "Test1", 3.5]
]
}
我已经使用站点 jsonschema.net 生成了架构如下
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://jsonschema.net",
"type": "object",
"properties": {
"Data": {
"id": "http://jsonschema.net/Data",
"type": "array",
"items": [
{
"id": "http://jsonschema.net/Data/0",
"type": "array",
"items": [
{
"id": "http://jsonschema.net/Data/0/0",
"type": "integer"
},
{
"id": "http://jsonschema.net/Data/0/1",
"type": "string"
},
{
"id": "http://jsonschema.net/Data/0/2",
"type": "number"
}
],
"required": [
"0",
"1",
"2"
]
},
{
"id": "http://jsonschema.net/Data/1",
"type": "array",
"items": [
{
"id": "http://jsonschema.net/Data/1/0",
"type": "integer"
},
{
"id": "http://jsonschema.net/Data/1/1",
"type": "string"
},
{
"id": "http://jsonschema.net/Data/1/2",
"type": "number"
}
]
}
],
"required": [
"0",
"1"
]
}
},
"required": [
"Data"
]
}
如果您看到,它正在为每个相似类型的元组创建模式。请帮助我创建一个模式以通用方式验证每个元组。元组计数可能会有所不同。
最佳答案
如果您希望内部数组包含同类的所有项目,您可以使用对象而不是数组。以下架构验证您的示例:
{
"type" : "object",
"properties" : {
"Data" : {
"type" : "array",
"items" : {
"type" : "array",
"items" : [{
"type" : "integer"
}, {
"type" : "string"
}, {
"type" : "number"
}
]
}
}
}
}
我测试过了here .
关于javascript - 元组数组的 JSON 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34334376/