元组数组的JSON架构
JSON Schema for Array of tuples
提前感谢。
我是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"
}
]
}
}
}
}
我在这里测试过。
JSON模式为元组提供了一种新的语法,jruizaranguren之前提出的解决方案现在可以用这种方式更精确地编写:
{
"type": "object",
"properties": {
"Data": {
"type": "array",
"items": [
{
"type": "array",
"prefixItems": [
{ "type": "integer" },
{ "type": "string" },
{ "type": "integer" }
],
"minItems": 3,
"items": false
}
]
}
},
"required": [
"Data"
]
}
{
"Table1": {
"Data": [
[
100,
"Test",
2.5
],
[
101,
"Test1",
5.5
]
]
}
}
上面是示例json&其模式如下
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://jsonschema.net",
"type": "object",
"properties": {
"Table1": {
"id": "http://jsonschema.net/Table1",
"type": "object",
"properties": {
"Data": {
"id": "http://jsonschema.net/Table1/Data",
"type": "array",
"items": {
"type": "array",
"items": [
{
"id": "http://jsonschema.net/Table1/Data/0/0",
"type": "integer"
},
{
"id": "http://jsonschema.net/Table1/Data/0/1",
"type": "string"
},
{
"id": "http://jsonschema.net/Table1/Data/0/2",
"type": "number"
}
],
"additionalItems": false,
"required": [
"0",
"1",
"2"
]
}
}
},
"required": [
"Data"
]
}
}
}
此模式适用于所有数据行,但其必需的属性无法正常工作。尽管我期待所有3列的数据。它也接受具有1或2列的行。如果有人知道的话。请纠正我。
[ 101 ], [ 101, "TEST3" ]
也是不期望的数据的有效记录。
相关文章:
- jQuery匹配JSON对象的部分文本
- 在循环中分配json值时,值被覆盖
- 需要帮助设置json数组
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 使用JQuery解析JSON嵌套数组
- 测试两个版本的 json 架构是否向后兼容
- 如何根据“架构”过滤 JSON 对象
- 在 php 中验证 json 架构
- 将 JSON 解析为具有定义的“架构”的对象
- 将 HTML 表单架构保存为 JSON
- express.js - 最佳 POST JSON 架构验证器
- 根据 JSON 文件获取对象的架构
- chrome打包应用程序中的JSON架构验证
- 元组数组的JSON架构
- 转换JSON架构格式
- 通过REST端点用JSON对象更新Mongoose架构
- JSON架构:读取带有require的文件时,会删除多个$ref