Nit*_*inK 3 javascript json json.net jsonschema
提前致谢.
我是JSON和JSON架构的新手.试图为元组数组生成JSON模式.但它没有验证多个记录,如所有类似类型的元组的循环.下面是json样本.
{
"Data":
[
[ 100, "Test", 2.5 ],
[ 101, "Test1", 3.5]
]
}
Run Code Online (Sandbox Code Playgroud)
我使用网站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"
]
}
Run Code Online (Sandbox Code Playgroud)
如果你看到,它正在为每个类似类型的元组创建模式.请帮我创建一个模式,以通用的方式验证每个元组.元组数可能会有所不同.
如果希望内部数组具有相同类型的所有项,则可以使用对象而不是数组.以下架构验证了您的示例:
{
"type" : "object",
"properties" : {
"Data" : {
"type" : "array",
"items" : {
"type" : "array",
"items" : [{
"type" : "integer"
}, {
"type" : "string"
}, {
"type" : "number"
}
]
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里测试过它.
JSON模式具有新的元组语法,jruizaranguren 之前建议的解决方案现在可以通过以下方式更精确地编写:
{
"type": "object",
"properties": {
"Data": {
"type": "array",
"items": [
{
"type": "array",
"prefixItems": [
{ "type": "integer" },
{ "type": "string" },
{ "type": "integer" }
],
"minItems": 3,
"items": false
}
]
}
},
"required": [
"Data"
]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3087 次 |
| 最近记录: |