我试图在 JSON 模式中定义以下内容:
"codenumber": {
"12345": [
{
"prop1": "yes",
"prop2": "no"
}
]
}
Run Code Online (Sandbox Code Playgroud)
codenumber 对象包含一个属性“12345”,它始终是一个包含数组的字符串编号。然而,数值可以改变,所以我不能像这样简单地定义它:
"codenumber": {
"type": "object",
"properties": {
"12345": {
"type": "array",
"items": {
"type": "object",
"properties": {
"prop1": { "type": "string" },
"prop2": { "type": "string" }
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何方式我都可以将第一个属性名称定义为任何类型的字符串?
您可以使用“patternProperties”而不是“properties”:
"codenumber": {
"type": "object",
"patternProperties": {
"^[0-9]+$": {
"type": "array",
"items": {
"type": "object",
"properties": {
"prop1": { "type": "string" },
"prop2": { "type": "string" }
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)