[英]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" ]
也是預期之外的有效數據記錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.