gpt4 book ai didi

java - String 类型的属性与以下类型不匹配 : object in schema JSON Schema validator

转载 作者:行者123 更新时间:2023-11-30 05:24:25 26 4
gpt4 key购买 nike

我正在尝试为我的 JSON 对象编写 JSON 架构,但无法跟踪错误。我希望我的 JSON 对象在 Java 中以以下方式存储:

public class Category {
private Map<String, List<String>> categoryMapping;
}

示例 JSON:

{
"categoryMapping": {
"categoryA": ["a","b","c"],
"categoryB": ["x","y","z"],
"categoryC": ["x","y","z"]
}
}

但是,如果我按以下方式编写架构:

{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"title": "$id$",
"description": "list_of_values-1",
"required": [
"categoryMapping"
],
"properties": {
"categoryMapping": {
"$id": "#/properties/categoryMapping",
"type": "object",
"title": "The categoryMapping Schema",
"properties": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}

我收到以下错误:字符串类型的属性“#/properties/categoryMapping/properties/type”与以下类型不匹配:架构中的对象 http://json-schema.org/draft-04/schema#

但是如果我指定它起作用的类别类型:

"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"title": "$id$",
"description": "list_of_values-1",
"properties": {
"categoryMapping": {
"$id": "#/properties/categoryMapping",
"type": "object",
"title": "The Categorymapping Schema",
"required": [
"categoryA",
"categoryB",
"categoryC"
],
"properties": {
"categoryA": {
"$id": "#/properties/categoryMapping/properties/categoryA",
"type": "array",
"title": "The Categorya Schema",
"items": {
"$id": "#/properties/categoryMapping/properties/categoryA/items",
"type": "string",
"title": "The Items Schema",
"default": "",
"examples": [
"a",
"b",
"c"
],
"pattern": "^(.*)$"
}
},
"categoryB": {
"$id": "#/properties/categoryMapping/properties/categoryB",
"type": "array",
"title": "The Categoryb Schema",
"items": {
"$id": "#/properties/categoryMapping/properties/categoryB/items",
"type": "string",
"title": "The Items Schema",
"default": "",
"examples": [
"x",
"y",
"z"
],
"pattern": "^(.*)$"
}
},
"categoryC": {
"$id": "#/properties/categoryMapping/properties/categoryC",
"type": "array",
"title": "The Categoryc Schema",
"items": {
"$id": "#/properties/categoryMapping/properties/categoryC/items",
"type": "string",
"title": "The Items Schema",
"default": "",
"examples": [
"x",
"y",
"z"
],
"pattern": "^(.*)$"
}
}
}
}
}
}

有没有一种方法可以在不显式指定所有类别类型的列表的情况下编写架构?

最佳答案

因此,您的示例 JSON 实际上是具有三个属性的类型,这就是为什么您生成的架构要求您显式定义每个属性,即使它们实际上属于同一类型。

但是,如果您愿意稍微修改示例 json:

{
"categoryMapping": [
{
"name": "categoryA",
"map": ["a","b","c"]
},
{
"name": "categoryB",
"map": ["x","y","z"]
},
{
"name": "categoryC",
"map": ["x","y","z"]
}
]
}

然后您可以使用以下架构验证它:

{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"categoryMapping": {
"type": "array",
"items": {
"type": "object",
"required": [
"name",
"map"
],
"properties": {
"name": {
"type": "string"
},
"map": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
}
}

由于您可以指定数组中允许的最小和最大项目数,因此如果您愿意,也可以将类别数限制为 3 个,将“ map ”数限制为 3 个。

关于java - String 类型的属性与以下类型不匹配 : object in schema JSON Schema validator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58948223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com