gpt4 book ai didi

java - Json 架构验证失败

转载 作者:行者123 更新时间:2023-12-02 01:19:35 25 4
gpt4 key购买 nike

我的 pom.xml 中有以下依赖项

 <!-- https://github.com/everit-org/json-schema -->
<dependency>
<groupId>com.github.everit-org.json-schema</groupId>
<artifactId>org.everit.json.schema</artifactId>
<version>1.11.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>

这是我的 json 架构

{
"$schema": "http://json-schema.org/draft-06/schema#",
"id": "test",
"title": "test-json validation",
"description": "This schema should define the structure of the test json",
"allOf": [
{
"$ref": "classpath:/jsonSchema/header/test.json#/definitions/test"
},
{
"$ref": "classpath:/jsonSchema/rows/test.json#/definitions/test"
}
],
"properties": {
"version": {
"type": "array",
"items": {
"type": "string",
"enum": [
"2.0",
"2.1"
]
}
}
},
"required": [
"version"
]
}

这就是我想要实现的目标

public Schema createSchema(String schemaPath) throws IOException {

Schema schema = null;
try (InputStream inputStream = new ClassPathResource(schemaPath).getInputStream()) {
JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
schema = SchemaLoader.load(rawSchema);
}
return schema;
}

我得到以下异常:

SchemaException: classpath:/jsonSchema/header/order_header.json#/definitions/order_header/properties/order_header/properties/origin/properties/locale: expected type is one of Boolean or JsonObject, found: String at org.everit.json.schema.loader.LoadingState.createSchemaException(LoadingState.java:142) at org.everit.json.schema.loader.JsonValue$Multiplexer.multiplexFailure(JsonValue.java:50) at org.everit.json.schema.loader.JsonValue$Multiplexer.lambda$requireAny$1(JsonValue.java:45) at java.util.Optional.orElseThrow(Optional.java:290) at org.everit.json.schema.loader.JsonValue$Multiplexer.requireAny(JsonValue.java:45) at org.everit.json.schema.loader.SchemaLoader.load(SchemaLoader.java:434) [6x]

最佳答案

properties 对象中的值必须是架构。

就您而言,您已将“string”作为值。

您看到的错误是 SCHEMA 无法验证,因为它期望看到 boolean 值或 JSON 对象,但获取的是 locale 属性值的字符串。

关于java - Json 架构验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57957794/

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