java 如何以编程方式针对模式验证 JSON 字符串?

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

How can I validate a JSON string against a schema programmatically?

java.netjsonvalidation

提问by

Are there simple libraries out there (.NET and Java) that are able to validate a JSON string given a schema definition?

是否有简单的库(.NET 和 Java)能够在给定模式定义的情况下验证 JSON 字符串?

It should work similar to how XML can be validated using XSD.

它的工作方式应该类似于使用 XSD 验证 XML 的方式。

EDIT:I need .NET andJava libraries. Thanks to darin for the .NET link!

编辑:我需要 .NETJava 库。感谢 darin 提供 .NET 链接!

采纳答案by Darin Dimitrov

For .NET you could use Json.NETwhich supports schema validation.

对于 .NET,您可以使用支持架构验证的Json.NET

回答by Dawid

HereYou can find some Java classes which might helpful when working with JSON.

在这里您可以找到一些在使用 JSON 时可能会有所帮助的 Java 类。

When syntax of Your JSON String is incorrect instance of JSONExceptionwill be thrown - this is the simplest way to check correctness, I think. JSONTokeneris the class You have to use to parse. It has a constructor which takes java.lang.Stringas a parameter.

当您的 JSON 字符串的语法不正确时,将抛出JSONException实例- 我认为这是检查正确性的最简单方法。JSONTokener是您必须用来解析的类。它有一个以java.lang.String作为参数的构造函数。

Edit:Sorry, I misunderstood a little. Probably JSON Toolsis the library You are looking for. It provides validation using a JSON schema.

编辑:对不起,我误解了一点。JSON Tools可能是您正在寻找的库。它使用 JSON 模式提供验证

回答by Leopd

www.json-schema.org defines a standard for implementing schemas in JSON. There is a list of software including validators in many languages here:

www.json-schema.org 定义了在 JSON 中实现模式的标准。这里有一个软件列表,包括多种语言的验证器:

http://json-schema.org/implementations.html

http://json-schema.org/implementations.html

回答by Tomas

I've used NJsonSchema for .NET library recently for validation against a schema and it worked perfectly for me, even for complex schemes with definitions in several files:

我最近使用 NJsonSchema for .NET library 来验证模式,它对我来说非常有用,即使对于在多个文件中定义的复杂方案也是如此:

https://github.com/NJsonSchema/NJsonSchema

https://github.com/NJsonSchema/NJsonSchema

NJsonSchema is a .NET library to read, generate and validate JSON Schema draft v4 schemas. The library can read a schema from a file or string and validate JSON data against it. A schema can also be generated from an existing .NET class. With the code generation APIs you can generate C# and TypeScript classes or interfaces from a schema.

NJsonSchema 是一个 .NET 库,用于读取、生成和验证 JSON Schema 草案 v4 模式。该库可以从文件或字符串中读取模式并根据它验证 JSON 数据。也可以从现有的 .NET 类生成架构。使用代码生成 API,您可以从架构生成 C# 和 TypeScript 类或接口。

It allows not only validate against schema, but also to generate schema files from existing classes. The advantage is that it is an open source and the communication with the author of the project was very quick and good in case of my questions.

它不仅允许针对模式进行验证,还允许从现有类生成模式文件。优点是它是开源的,如果我有问题,与项目作者的沟通非常快速和良好。

It can be a good choice for someone looking for a free solution for validation.

对于寻找免费验证解决方案的人来说,它可能是一个不错的选择。