gpt4 book ai didi

java - JSON 架构引用

转载 作者:行者123 更新时间:2023-12-02 10:50:58 24 4
gpt4 key购买 nike

我在让我的代码再次运行时遇到问题。可悲的是它可以工作,但我不知道为什么它现在不起作用。

加载架构的代码示例:

// ----------------- JSON Schema -----------------
jsonSchema = new File("src/main/java/de/project/jsonvalidator/test1/test.json");
final URI uri = jsonSchema.toURI();
System.out.println("URI = " + uri.toString());
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
final com.github.fge.jsonschema.main.JsonSchema schema = factory.getJsonSchema(uri.toString());
// ----------------- JSON Schema -----------------


json 架构的主文件:

{
"$schema": "http://json-schema.org/draft-04/schema#",
"description" : "schema validating people and vehicles",
"type" : "object",
"properties": {
"billing_address": { "$ref": "MyBoolean.json#/MyBool" },
"shipping_address": { "$ref": "MyBoolean_1.json#/MyBoolABC" }
}
}


对其他架构文件的引用将无法解析!

我按照链接中的说明进行操作: java json schema validation relative path not working (URI not found)

有人知道如何以相对方式解析引用吗?

@萨比尔·汗
我对 json 模式文件没有进行任何更改!我只是更改了一些代码行的顺序。我没有得到任何异常(exception)。它只是不能解决引用问题。

之前:

    ProcessingReport report = null;
boolean result = false;
File jsonSchema = null;
File jsonData = null;
try {
jsonSchema = new File("./src/main/java/de/project/jsonvalidator/test1/test.json");
final URI uri = jsonSchema.toURI();
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();

jsonData = new File("./src/main/java/de/project/jsonvalidator/test1/data.json");
JsonNode data = JsonLoader.fromFile(jsonData);
final com.github.fge.jsonschema.main.JsonSchema schema = factory.getJsonSchema(uri.toString());

report = schema.validate(data, true);

System.out.println("Success = " + report.isSuccess());
Iterator<ProcessingMessage> it = report.iterator();
while(it.hasNext())
System.out.println("msg = " + it.next().getMessage());

} catch (JsonParseException jpex) {
System.out.println("Error. Something went wrong trying to parse json data: #<#<"
+ jsonData
+ ">#># or json schema: @<@<"
+ jsonSchema
+ ">@>@. Are the double quotes included? "+jpex.getMessage());
jpex.printStackTrace();

} catch (ProcessingException pex) {
System.out.println("Error. Something went wrong trying to process json data: #<#<"
+ jsonData
+ ">#># with json schema: @<@<"
+ jsonSchema
+ ">@>@ "+pex.getMessage());
pex.printStackTrace();

} catch (IOException e) {
System.out.println("Error. Something went wrong trying to read json data: #<#<"
+ jsonData
+ ">#># or json schema: @<@<"
+ jsonSchema
+ ">@>@");
e.printStackTrace();

} catch ( Exception ex) {
ex.printStackTrace();
}


之后:

    ProcessingReport report = null;
boolean result = false;
File jsonSchema = null;
File jsonData = null;
try {
//File jsonSchema = new File("./src/main/java/de/project/jsonvalidator/test/test.json");

// ----------------- JSON Schema -----------------
jsonSchema = new File("src/main/java/de/project/jsonvalidator/test1/test.json");
final URI uri = jsonSchema.toURI();
System.out.println("URI = " + uri.toString());
//JsonNode jnSchema = JsonLoader.fromFile(jsonSchema);
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
final com.github.fge.jsonschema.main.JsonSchema schema = factory.getJsonSchema(uri.toString());
// ----------------- JSON Schema -----------------


// ----------------- JSON Daten -----------------
jsonData = new File("src/main/java/de/project/jsonvalidator/test1/data.json");
JsonNode data = JsonLoader.fromFile(jsonData);
// ----------------- JSON Daten -----------------


// ----------------- JSON Validierung -----------------
//boolean ret = schema.validInstance(jnSchema);
report = schema.validate(data, true);
// ----------------- JSON Validierung -----------------


// ----------------- JSON Auswertung -----------------
//System.out.println("ret = " + ret);
System.out.println("Success = " + report.isSuccess());
Iterator<ProcessingMessage> it = report.iterator();
while(it.hasNext())
System.out.println("msg = " + it.next().getMessage());
// ----------------- JSON Auswertung -----------------



} catch (JsonParseException jpex) {
System.out.println("Error. Something went wrong trying to parse json data: #<#<"
+ jsonData
+ ">#># or json schema: @<@<"
+ jsonSchema
+ ">@>@. Are the double quotes included? "+jpex.getMessage());
jpex.printStackTrace();

} catch (ProcessingException pex) {
System.out.println("Error. Something went wrong trying to process json data: #<#<"
+ jsonData
+ ">#># with json schema: @<@<"
+ jsonSchema
+ ">@>@ "+pex.getMessage());
pex.printStackTrace();

} catch (IOException e) {
System.out.println("Error. Something went wrong trying to read json data: #<#<"
+ jsonData
+ ">#># or json schema: @<@<"
+ jsonSchema
+ ">@>@");
e.printStackTrace();

} catch ( Exception ex) {
ex.printStackTrace();
}


MyBoolean.json

{
"MyBool": {
"type": "object",
"properties": {
"value" :{
"type": "string",
"enum": [ "true", "false", "file not found" ]
}
},
"required": ["value"],
"additionalProperties": false
}
}


这是 MyBoolean_1.json 文件:

{
"MyBoolABC": {
"type": "object",
"properties": {
"value1" :{
"type": "string",
"enum": [ "true", "false", "file not found" ]
}
},
"required": ["value1"],
"additionalProperties": false
}
}

最佳答案

我找到了问题的答案。

我将代码改回:

jsonSchema = new File("./src/main


接下来我发现的是:

如果我将架构文件更改为,解析器不会抛出任何错误消息

{
"$schema": "http://json-schema.org/draft-04/schema#",
"description" : "schema validating people and vehicles",
"type" : "object",
"properties": {
"billing_address": { "$ref": "MyBoolean.json#/MyBool" }
}
}

并且 data.json 仍然保持不变!

解析器是否有可能告诉我们数据文件中有附加信息,但模式文件中没有描述!?

关于java - JSON 架构引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34463138/

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