如何在 Java 中验证 JSON 模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48295686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:48:46  来源:igfitidea点击:

How to validate JSON schema in java?

javajsonjsonschema

提问by Obeth Samuel

I try to check user detail object with use JSON schema. But I don't know how to check JSON object in Java.

我尝试使用 JSON 模式检查用户详细信息对象。但我不知道如何在 Java 中检查 JSON 对象。

My Schema:

我的架构:

{
     "type" : "object",
     "properties" : {
     "first_name" : {
                     "type" : "string" , 
                     "minLength"  : 3 , 
                     "maxLength" : 255 
                  }, 
   "last_name" : {
                     "type" : "string" , 
                     "minLength"  : 3 , 
                     "maxLength" : 255 
                  },
    "age"       : { 
                     "type" : "integer" , 
                     "minimum" : 16 ,
                      "maximum" : 40
                  },
    "phone_number" : {
                        "type" : "integer",
                        "pattern" : "[6-9][0-9]{9}"
                     } ,
     "email"     : { 
                       "type" : "string",
                       "pattern" : "[a-z0-9]+"
                   } , 
     "password" : { 
                       "type" : "string" ,
                       "minLength" : 7 ,
                       "maxLength" : 255 ,
                       "pattern" : "^.{7, 255}$"
                  } , 
      "gender" : { "enum" : ["Male" , "Female"] }   
},

"required" : ["first_name","last_name" , "age"  ,"email" , "password" 
, "gender" ]
}

My Sample Input:

我的样本输入:

{
"first_name" : "Sample" ,
"last_name" : "Name" ,
"age"  : 19,
"gender" : "Male",
"phone_number"  :  9080245591,
"email" : "[email protected]",
"password" : "uni=versity"
}

Any one can say how to check this input with use JSON schema in Java.

任何人都可以说如何使用 Java 中的 JSON 模式检查此输入。

采纳答案by pdem

You can use FasterXML Hymanson with the module:json-schema-validator as holten proposed.

您可以按照 holten 的建议将 FasterXML Hymanson 与 module:json-schema-validator 一起使用。

Include it in maven: com.github.java-json-tools json-schema-validator 2.2.8

将它包含在 maven 中:com.github.java-json-tools json-schema-validator 2.2.8

Complementary to this, to generate schema from object instead of writing it manually you can also use another module: https://github.com/FasterXML/Hymanson-module-jsonSchema

作为补充,要从对象生成模式而不是手动编写它,您还可以使用另一个模块:https: //github.com/FasterXML/Hymanson-module-jsonSchema

I may add a functional example if needed

如果需要,我可以添加一个功能示例

回答by holten

The json-schema-validatorin GitHub, Perhaps It will help you to check the json object in java.

GitHub 中的json-schema-validator,或许可以帮你检查 java 中的 json 对象。