【问题标题】:Generate JSON Schema from JSON using Java使用 Java 从 JSON 生成 JSON Schema
【发布时间】:2019-11-30 07:23:49
【问题描述】:

是否有任何 Java 包可用于将 JSON 字符串转换为 JSON 模式?我在网上查过。我在 Python、Ruby 和 NodeJS 中找到了库,但在 Java 中没有找到

所有 Java 库都从 POJO 生成 JSON 模式

【问题讨论】:

    标签: java jsonschema


    【解决方案1】:

    我认为你可以在 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");
        }
    

    【讨论】:

      猜你喜欢
      • 2014-01-17
      • 1970-01-01
      • 2022-01-17
      • 2018-09-23
      • 1970-01-01
      • 2015-03-07
      • 2016-01-13
      • 2014-03-20
      • 1970-01-01
      相关资源
      最近更新 更多