感谢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
享受!