简体   繁体   English

使用 Java 从 JSON 生成 JSON Schema

[英]Generate JSON Schema from JSON using Java

Is there any Java package which I can use to convert a JSON string to JSON schema?是否有任何 Java 包可用于将 JSON 字符串转换为 JSON 模式? I have looked up online.我在网上查过。 I found libraries in Python, Ruby and NodeJS, but not in Java我在 Python、Ruby 和 NodeJS 中找到了库,但在 Java 中没有

All the Java libraries generate JSON schema from a POJO所有 Java 库都从 POJO 生成 JSON 模式

I think that you can try this library on github it does exactly what you want from a JSON, You only need to build it and use json-string-schema-generator我认为你可以在 github 上尝试这个库,它完全符合你想要的 JSON,你只需要构建它并使用json-string-schema-generator

String json = "{\"sectors\": [{\"times\":[{\"intensity\":30," +
                "\"start\":{\"hour\":8,\"minute\":30},\"end\":{\"hour\":17,\"minute\":0}}," +
                "{\"intensity\":10,\"start\":{\"hour\":17,\"minute\":5},\"end\":{\"hour\":23,\"minute\":55}}]," +
                "\"id\":\"dbea21eb-57b5-44c9-a953-f61816fd5876\"}]}";
        String result = JsonSchemaGenerator.outputAsString("Schedule", "this is a test", json);
            /* sample output
            {
              "title": "Schedule",
              "description": "this is a test",
              "type": "object",
              "properties": {
                "sectors": {
                  "type": "array",
                  "items": {
                    "properties": {
                      "times": {
                        "type": "array",
                        "items": {
                          "properties": {
                            "intensity": {
                              "type": "number"
                            },
                            "start": {
                              "type": "object",
                              "properties": {
                                "hour": {
                                  "type": "number"
                                },
                                "minute": {
                                  "type": "number"
                                }
                              }
                            },
                            "end": {
                              "type": "object",
                              "properties": {
                                "hour": {
                                  "type": "number"
                                },
                                "minute": {
                                  "type": "number"
                                }
                              }
                            }
                          }
                        }
                      },
                      "id": {
                        "type": "string"
                      }
                    }
                  }
                }
              } 
            }
             */

        // To generate JSON schema into a file
        JsonSchemaGenerator.outputAsFile("Schedule", "this is a test", json, "output-schema.json");

        // To generate POJO(s)
        JsonSchemaGenerator.outputAsPOJO("Schedule", "this is a test", json, "com.example", "generated-sources");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM