使用 java 进行 JSON 模式验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2499126/
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 validation using java
提问by tomdee
I'm writing some acceptance tests for a java webapp that returns JSON objects. I would like to verify that the JSON returned validates against a schema. Can anyone suggest any tools for this?
我正在为返回 JSON 对象的 java webapp 编写一些验收测试。我想验证返回的 JSON 是否针对架构进行了验证。任何人都可以为此建议任何工具吗?
采纳答案by Simon Lieschke
The JSON Tools project(Programmer's Guide) includes a tool to validate the contents of a JSON file using a JSON schema.
该JSON工具项目(程序员指南)包括验证使用JSON模式的JSON文件的内容的工具。
An alternative could be to validate running the (JavaScript) JSON Schema Validatorusing Rhino.
另一种方法是使用Rhino验证运行(JavaScript)JSON Schema Validator。
回答by Fredrik
The json-schema-validator(currently in version 0.0.1, so it's in a pre-alpha state) worked pretty well for me. Be aware that it is not 100% feature complete but it could still correctly identify a lot of errors in my json content.
该JSON-架构验证(目前0.0.1版本,因此它在预阿尔法状态)的工作很适合我。请注意,它不是 100% 功能完整,但它仍然可以正确识别我的 json 内容中的很多错误。
回答by Boro
@b.long I came across this post looking for a very particular solution to easily verify if a String's content has a JSON (object/array). I couldn't find any library that would suit my needs.
@b.long 我看到这篇文章是为了寻找一个非常特殊的解决方案来轻松验证字符串的内容是否具有 JSON(对象/数组)。我找不到任何适合我需要的图书馆。
JSON Tools project or json-schema-validator though offer a lot are too big for my needs. Thus I did implement my own solutionthat is build on top of JSON.org reference implementation of JSON in Java. I have already been using the JSON.org's code and technically all this functionality was already there, thus I added this very simple functionality reusing its code.
JSON 工具项目或 json-schema-validator 虽然提供了很多对于我的需求来说太大了。因此,我确实实现了我自己的解决方案,该解决方案构建在JSON.org 中 Java 中 JSON 的参考实现之上。我已经在使用 JSON.org 的代码,从技术上讲,所有这些功能都已经存在,因此我添加了这个非常简单的功能,重用了它的代码。
I use it to easily test if the String returned by methods querying my database are formatted in proper JSON which otherwise if badly formatted and posted to a client might cause it to stop working.
我使用它来轻松测试查询我的数据库的方法返回的字符串是否以正确的 JSON 格式设置,否则如果格式错误并发布到客户端可能会导致它停止工作。
Hope this is of use to you as it is to me.
希望这对你和我一样有用。