Java JSON 模式验证器库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18950047/
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
JSON schema validator library
提问by user1457881
I have a restful web service(JAVA) which has to accept JSON requests. I have to first validate this JSON against a JSON schema that I have. I'm not sure what is the best JAVA library to validate JSON again JSON schemas. I have used json-schema-validator-2.1.7 library but it has not been very helpful. Even thought my JSON is not a valid JSON I do not get any errors.
我有一个宁静的网络服务(JAVA),它必须接受 JSON 请求。我必须首先根据我拥有的 JSON 模式验证这个 JSON。我不确定什么是再次验证 JSON JSON 模式的最佳 JAVA 库。我已经使用了json-schema-validator-2.1.7 库,但它并不是很有帮助。即使认为我的 JSON 不是有效的 JSON,我也没有收到任何错误。
Here is the code I use for json-schema-validator-2.1.7
这是我用于 json-schema-validator-2.1.7 的代码
InputStream jsonSchemaInputStream = Assessment.class.getClassLoader().getResourceAsStream("Schemas/AssessmentMetrics.json");
ObjectMapper mapper = new ObjectMapper();
// Allows to retrieve a JSONSchema object on various sources
// supported by the ObjectMapper provided
JSONSchemaProvider schemaProvider = new HymansonSchemaProvider(mapper);
// Retrieves a JSON Schema object based on a file
JSONSchema schema = schemaProvider.getSchema(jsonSchemaInputStream);
// Validates a JSON Instance object stored in a file
List<String> errors = schema.validate(contents);
回答by Dan
I'm biased with Hymanson for all things JSON.
对于 JSON 的所有事情,我都偏向于 Hymanson。