非常感谢 Douglas Crockford 和 Francis Galiegue 编写了基于 java 的 json 模式处理器!http://json-schema-validator.herokuapp.com/index.jsp的在线测试器很棒!我真的很喜欢有用的错误消息(我只发现了一个失败的示例),尽管行和列和/或上下文会更好(现在,您只能在 JSON 格式错误期间获得行和列信息(由 Jackson 提供) ). 最后,我要感谢 Michael Droettboom 的精彩教程(即使他只涵盖了 Python、Ruby 和 C,而明显地忽略了所有语言中最好的语言 :-))。
对于那些错过它的人(就像我一开始所做的那样),在 github.com/fge/json-schema-processor-examples 上有一些示例。虽然这些示例令人印象深刻,但它们并不是最初要求的简单 json 验证示例(我也在寻找)。简单示例位于 github.com/fge/json-schema-validator/blob/master/src/main/java/com/github/fge/jsonschema/examples/Example1.java
上面 Alex 的代码对我不起作用,但很有帮助;我的 pom 正在提取最新的稳定版本 2.0.1,并在我的 maven pom.xml 文件中插入了以下依赖项:
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>json-schema-validator</artifactId>
<version>2.0.1</version>
</dependency>
然后下面的java代码对我来说很好:
import java.io.IOException;
import java.util.Iterator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.report.ProcessingMessage;
import com.github.fge.jsonschema.report.ProcessingReport;
import com.github.fge.jsonschema.util.JsonLoader;
public class JsonValidationExample
{
public boolean validate(String jsonData, String jsonSchema) {
ProcessingReport report = null;
boolean result = false;
try {
System.out.println("Applying schema: @<@<"+jsonSchema+">@>@ to data: #<#<"+jsonData+">#>#");
JsonNode schemaNode = JsonLoader.fromString(jsonSchema);
JsonNode data = JsonLoader.fromString(jsonData);
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonSchema schema = factory.getJsonSchema(schemaNode);
report = schema.validate(data);
} catch (JsonParseException jpex) {
System.out.println("Error. Something went wrong trying to parse json data: #<#<"+jsonData+
">#># or json schema: @<@<"+jsonSchema+">@>@. Are the double quotes included? "+jpex.getMessage());
//jpex.printStackTrace();
} catch (ProcessingException pex) {
System.out.println("Error. Something went wrong trying to process json data: #<#<"+jsonData+
">#># with json schema: @<@<"+jsonSchema+">@>@ "+pex.getMessage());
//pex.printStackTrace();
} catch (IOException e) {
System.out.println("Error. Something went wrong trying to read json data: #<#<"+jsonData+
">#># or json schema: @<@<"+jsonSchema+">@>@");
//e.printStackTrace();
}
if (report != null) {
Iterator<ProcessingMessage> iter = report.iterator();
while (iter.hasNext()) {
ProcessingMessage pm = iter.next();
System.out.println("Processing Message: "+pm.getMessage());
}
result = report.isSuccess();
}
System.out.println(" Result=" +result);
return result;
}
public static void main(String[] args)
{
System.out.println( "Starting Json Validation." );
JsonValidationExample app = new JsonValidationExample();
String jsonData = "\"Redemption\"";
String jsonSchema = "{ \"type\": \"string\", \"minLength\": 2, \"maxLength\": 11}";
app.validate(jsonData, jsonSchema);
jsonData = "Agony"; // Quotes not included
app.validate(jsonData, jsonSchema);
jsonData = "42";
app.validate(jsonData, jsonSchema);
jsonData = "\"A\"";
app.validate(jsonData, jsonSchema);
jsonData = "\"The pity of Bilbo may rule the fate of many.\"";
app.validate(jsonData, jsonSchema);
}
}
我从上面的代码得到的结果是:
Starting Json Validation.
Applying schema: @<@<{ "type": "string", "minLength": 2, "maxLength": 11}>@>@ to data: #<#<"Redemption">#>#
Result=true
Applying schema: @<@<{ "type": "string", "minLength": 2, "maxLength": 11}>@>@ to data: #<#<Agony>#>#
Error. Something went wrong trying to parse json data: #<#<Agony>#># or json schema: @<@<{ "type": "string", "minLength": 2, "maxLength": 11}>@>@. Are the double quotes included?
Result=false
Applying schema: @<@<{ "type": "string", "minLength": 2, "maxLength": 11}>@>@ to data: #<#<42>#>#
Processing Message: instance type does not match any allowed primitive type
Result=false
Applying schema: @<@<{ "type": "string", "minLength": 2, "maxLength": 11}>@>@ to data: #<#<"A">#>#
Processing Message: string is too short
Result=false
Applying schema: @<@<{ "type": "string", "minLength": 2, "maxLength": 11}>@>@ to data: #<#<"The pity of Bilbo may rule the fate of many.">#>#
Processing Message: string is too long
Result=false
享受!