【发布时间】:2026-02-01 09:45:01
【问题描述】:
我需要帮助为可能是对象或对象数组的值创建 JSON 架构。
- lib: jsonschema==3.2.0
- py: 3.8
我有 2 个来自服务器的响应:
第一:
{
"result": [
{
"brand": "Test"
}
]}
秒:
{
"result":
{
"brand": "Test"
}
}
正如你所看到的,在第一种情况下,它是一个 obj 数组,第二个只是对象。
我的架构:
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"required": [
"result"
],
"properties": {
"result": {
"$id": "#/properties/result",
"type": ["array", "object"],
"additionalItems": true,
"items": {
"$id": "#/properties/result/items",
"anyOf": [
{
"$id": "#/properties/result/items/anyOf/0",
"type": "object",
"required": [
"brand"
],
"properties": {
"brand": {
"$id": "#/properties/result/items/anyOf/0/properties/brand",
"type": "string"
}
},
"additionalProperties": true
}
]
}
}
},
"additionalProperties": true}
第一种情况是返回数组,第二种情况是返回对象时检查“品牌”类型,否。
如何为一个字段“结果”设置两种类型以检查品牌类型?
【问题讨论】:
标签: python-3.x jsonschema