如何在Java中将示例JSON转换为JSON模式

4

我希望能将JSON文档转换为JSON模式。我在谷歌上搜索了一下,但没有找到符合我的要求的确切想法。

这里是JSON

 {
 "empId":1001,
 "firstName":"jonh",
 "lastName":"Springer",
 "title": "Engineer",
 "address": {
    "city": "Mumbai",
    "street": "FadkeStreet",
    "zipCode":"420125",
    "privatePhoneNo":{
            "privateMobile": "2564875421",
            "privateLandLine":"251201546"
    }
},
"salary": 150000,
"department":{
     "departmentId": 10521,
     "departmentName": "IT",
     "companyPhoneNo":{
             "cMobile": "8655340546",
             "cLandLine": "10251215465"
      },
     "location":{
             "name": "mulund",
             "locationId": 14500
      }
  }
}

我希望生成这样的内容
   {
   "$schema": "http://json-schema.org/draft-04/schema#",
   "type": "object",
   "title": "Employee",
   "properties": {
     "empId": {
           "type": "integer"
      },
      "firstName":{
           "type":"string"
      },
      "lastName": {
           "type": "string"
      },
      "title": {
           "type": "string"
     },
     "address": {
         "type": "object",
         "properties": {
                       "city": {
                                 "type": "string"
                        },
                        "street": {
                                 "type": "string"
                        },
                        "zipCode": {
                                 "type": "string"
                        },
                       "privatePhoneNo": {
                                 "type": "object",
                                 "properties": {
                                        "privateMobile": {
                                                 "type": "string"
                                         },
                                        "privateLandLine": {
                                                 "type": "string"
                                         }
                                   }
                          }
            }
      },
      "salary": {
            "type": "number"
      },
      "department": {
            "type": "object",
            "properties": {
                   "departmentId": {
                            "type": "integer"
                    },
                    "departmentName": {
                            "type": "string"
                    },
                    "companyPhoneNo": {
                            "type": "object",
                            "properties": {
                                         "cMobile": {
                                              "type": "string"
                                          },
                                         "cLandLine": {
                                              "type": "string"
                                          }
                              }
     },
     "location": {
                 "type": "object",
                 "properties": {
                               "name": {
                                     "type": "string"
                                 },
                              "locationId": {
                                     "type": "integer"
                               }
                   }
      }
    }
   }
 }
}

有没有类似这样的库,或者还有其他方法?


你正在将JSON转换为对象吗?如果是的话,这个链接可能会有所帮助:https://github.com/FasterXML/jackson-module-jsonSchema - rupesh_padhye
不需要 @rupesh_padhye。我想将JSON文档转换为JSON模式,还有其他方法吗? - Chaitanya Ghumare
2个回答

2

1

是的,像Garik Khachanyan一样,我想生成这个,但我想通过Java代码生成。我想创建一个实用程序,将JSON转换为JSON模式。 - Chaitanya Ghumare
我不理解如何从Java代码中使用json_schema_generator git hub? - Chaitanya Ghumare
请查看此处,这是Java相关的内容:https://github.com/reinert/JJSchema/blob/master/src/main/java/com/github/reinert/jjschema/JsonSchemaGenerator.java - Garik Khachanyan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接