Java JSON模式引用
我在让代码(再次)工作时遇到问题。可悲的是,它一直在工作,但我不知道为什么它现在不工作 加载架构的代码示例:Java JSON模式引用,java,json,jsonschema,ref,json-schema-validator,Java,Json,Jsonschema,Ref,Json Schema Validator,我在让代码(再次)工作时遇到问题。可悲的是,它一直在工作,但我不知道为什么它现在不工作 加载架构的代码示例: // ----------------- JSON Schema ----------------- jsonSchema = new File("src/main/java/de/project/jsonvalidator/test1/test.json"); final URI uri = jsonSchema.toURI(); System.out.println("URI = "
// ----------------- 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" }
}
}
对其他架构文件的引用将不会解析 我按照链接上的说明进行操作: 有人知道如何以相对的方式解析引用吗 @萨比尔·汗
我在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();
}
下面是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仍然保持不变!!
解析器是否有可能告诉我们在数据文件中有其他信息,但在模式文件中没有描述 哪一行失败,收到了什么错误消息?什么在起作用,什么在它停止工作之前发生了变化?编辑您的问题以提供这些详细信息。
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" }
}
}