JSON模式参考 - java

我的代码无法正常工作(再次)。可悲的是它正在运行,但是我不知道为什么它现在不起作用。

加载架构的代码示例:

// ----------------- 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模式文件上没有做任何更改!我只是更改了一些代码行的顺序。我没有任何例外。它只是不解决裁判。

之前:

    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 Map,如何将UTF-8字符串正确放置到地图? - java

我有一个地图,LinkedHashMap更确切地说。我想在上面放一个字符串对象。然后,我读取此值以查看实际存储的内容。字符串本身具有非ASCII字符(西里尔文,韩文等)。将其放到地图上然后阅读后,这些字符将替换为??? s。一些代码:Map obj = new LinkedHashMap(); System.out.println("name: &…

Java Double与BigDecimal - java

我正在查看一些使用双精度变量来存储(360-359.9998779296875)结果为0.0001220703125的代码。 double变量将其存储为-1.220703125E-4。当我使用BigDecimal时,其存储为0.0001220703125。为什么将它双重存储为-1.220703125E-4? 参考方案 我不会在这里提及精度问题,而只会提及数字…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

JSON SCHEMA PATTERN逗号分隔列表 - python

我的json模式中具有以下模式,并且我需要根据以下模式包含逗号分隔的值。当前模式只能像DV2一样处理一种模式所以我应该如何修改我的模式以包括多个字符串,如下所示,但它应该与声明的模式匹配。例如:“ DV2”,“ DEV1”,“ DEV3”,“ ST”, "ENVIRONMENT": { "type": "st…

Java-父类正在从子类中调用方法? - java

抱歉,我还是编码的新手,可能还没有掌握所有术语。希望您仍然能理解我的问题。我想得到的输出是:"Cost for Parent is: 77.77" "Cost for Child is: 33.33" 但是,我得到这个:"Cost for Parent is: 33.33" "Cost f…