我遇到问题让我的代码工作(再次)。可悲的是,它正在发挥作用,但我不知道为什么它现在不起作用。
加载架构的代码示例:
// ----------------- 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)
有人知道如何以相对的方式解析引用吗?
@Sabir Khan 我在json架构文件中没有改变任何东西!我只是改变了一些代码行的顺序。我没有任何例外。它只是没有解决参考。在:
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
}
}
答案 0 :(得分:0)
我找到了问题的答案。
我将代码更改回:
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仍然保持不变!!
是否有任何可能性,解析器告诉我们数据文件中还有其他信息,但架构文件中没有描述它!??