java 根据 XML 架构 (XSD) 验证 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36152972/
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-11-03 01:00:24  来源:igfitidea点击:

Validate JSON against XML Schema (XSD)

javajsonxmlxsdxml-validation

提问by user16655

Is it possible to validate JSON with an XSD in Java? I have an application where I receive JSON response, and I would like to validate it against existing XSD. Another part of my application uses XML, which is why it would be easiest if they both could validate against the existing XSD.

是否可以使用 Java 中的 XSD 验证 JSON?我有一个接收 JSON 响应的应用程序,我想根据现有的 XSD 对其进行验证。我的应用程序的另一部分使用 XML,这就是为什么如果它们都可以针对现有的 XSD 进行验证将是最简单的原因。

回答by kjhughes

No, XML Schema (XSD)is for validating XML; to validate JSON, see JSON Schema.

XML Schema (XSD)用于验证XML;要验证JSON,请参阅JSON 架构

I recommend generating schemas by hand for full understanding and full control over the constraints. However, here are some automated tools that can jumpstart the process:

我建议手动生成模式以完全理解和完全控制约束。但是,这里有一些可以快速启动该过程的自动化工具:

Related and also very useful:

相关且也非常有用:

  • To parse from XML to JSON (unmarshal) or serialize JSON to XML (marshal), see JSONIX.
  • For a list of implementations, including validators in various languages, see JSON-Schema Implementations.
  • 要将 XML 解析为 JSON(解组)或将 JSON 序列化为 XML(编组),请参阅JSONIX
  • 有关实现列表,包括各种语言的验证器,请参阅JSON-Schema 实现

回答by ingrid.e

No, the standards are different between the two.

不,两者的标准不同。

But if you really want to rely on the xsd for validating, Jsonix Schema Compilercan help you generate a JSON Schema to validate your json from your XML Schema.

但是,如果您真的想依靠 xsd 进行验证,Jsonix Schema Compiler可以帮助您生成 JSON Schema 以从您的 XML Schema 验证您的 json。