JSON Schema 数组怎么写?
数组用 type: "array" + items 描述元素。元素可以是标量,也可以是对象,甚至数组里套数组。
字符串数组
{
"type": "array",
"items": { "type": "string" }
}
对应 ["a", "b", "c"]。
数字数组
{ "type": "array", "items": { "type": "number" } }
对应 [1, 2.5, 3]。
对象数组
{
"type": "array",
"items": {
"type": "object",
"properties": {
"name": { "type": "string" },
"score": { "type": "integer" }
},
"required": ["name", "score"]
}
}
对应 [{ "name": "A", "score": 90 }, { "name": "B", "score": 85 }]。
嵌套数组(数组里套数组)
{ "type": "array", "items": { "type": "array", "items": { "type": "integer" } } }
对应 [[1,2],[3,4]]。
空数组怎么办
如果样例是 [],工具无从判断元素类型,会生成 { "type": "array", "items": {} }(items: {} 表示接受任意值)。想更精确,就给一个含元素的样例。