JSON 模式中的变量对象名称
Variable object names in JSON schema
我正在尝试在 JSON 架构中定义以下内容:
"codenumber": {
"12345": [
{
"prop1": "yes",
"prop2": "no"
}
]
}
codenumber 对象包含一个 属性“12345”,它始终是一个包含数组的字符串数字。但是数值可以改变,所以我不能简单地这样定义:
"codenumber": {
"type": "object",
"properties": {
"12345": {
"type": "array",
"items": {
"type": "object",
"properties": {
"prop1": { "type": "string" },
"prop2": { "type": "string" }
}
}
}
}
}
有什么方法可以将第一个 属性 名称定义为任何类型的字符串?
您可以使用 "patternProperties" 而不是 "properties":
"codenumber": {
"type": "object",
"patternProperties": {
"^[0-9]+$": {
"type": "array",
"items": {
"type": "object",
"properties": {
"prop1": { "type": "string" },
"prop2": { "type": "string" }
}
}
}
}
}
我正在尝试在 JSON 架构中定义以下内容:
"codenumber": {
"12345": [
{
"prop1": "yes",
"prop2": "no"
}
]
}
codenumber 对象包含一个 属性“12345”,它始终是一个包含数组的字符串数字。但是数值可以改变,所以我不能简单地这样定义:
"codenumber": {
"type": "object",
"properties": {
"12345": {
"type": "array",
"items": {
"type": "object",
"properties": {
"prop1": { "type": "string" },
"prop2": { "type": "string" }
}
}
}
}
}
有什么方法可以将第一个 属性 名称定义为任何类型的字符串?
您可以使用 "patternProperties" 而不是 "properties":
"codenumber": {
"type": "object",
"patternProperties": {
"^[0-9]+$": {
"type": "array",
"items": {
"type": "object",
"properties": {
"prop1": { "type": "string" },
"prop2": { "type": "string" }
}
}
}
}
}