转载自使用JSON Schema校验JSON数据是否合规,感谢博主的奉献!
- 用处
通过JSON Schema可按照规定json数据样式对json数据进行校验,可校验的包括json的键是否符合要求,值是否存在或格式是否符合要求等,具体格式可点击查看Json schema 格式; - 创建json数据对比文件,并存放至项目目录下,可通过工具自动生成json对比文件自动生成工具
在工具左侧json栏中粘贴json数据样例,点击INFER SCHEMA按钮即在右侧生成校验标准文件,复制保存为json格式文件(文件后缀为.json),样例如下:
原始数据:
{
"listexpressmail": [{
"procdate": "20170729",
"proctime": "091425",
"effect": "1",
"mailnum": "123433",
"description": "投递并签收",
"orgfullname": "深圳市揽投部",
"properdelivery": "10",
"action": "20",
"serialnumber": "20170729092356"
}]
}
工具自动生成校验文件:
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "The Root Schema",
"required": [
"listexpressmail"
],
"properties": {
"listexpressmail": {
"$id": "#/properties/listexpressmail",
"type": "array",
"title": "The Listexpressmail Schema",
"items": {
"$id": "#/properties/listexpressmail/items",
"type": "object",
"title": "The Items Schema",
"required": [
"procdate",
"proctime",
"effect",
"mailnum",
"description",
"orgfullname",
"properdelivery",
"action",
"serialnumber"
],
"properties": {
"procdate": {
"$id": "#/properties/listexpressmail/items/properties/procdate",
"type": "string",
"title": "The Procdate Schema",
"default": "",
"examples": [
"20170729"
],
"pattern": "^(.*)$"
},
"proctime": {
"$id": "#/properties/listexpressmail/items/properties/proctime",
"type": "string",
"title": "The Proctime Schema",
"default": "",
"examples": [
"091425"
],
"pattern": "^(.*)$"
},
"effect": {
"$id": "#/properties/listexpressmail/items/properties/effect",
"type": "string",
"title": "The Effect Schema",
"default": "",
"examples": [
"1"
],
"pattern": "^(.*)$"
},
"mailnum": {
"$id": "#/properties/listexpressmail/items/properties/mailnum",
"type": "string",
"title": "The Mailnum Schema",
"default": "",
"examples": [
"9783860440505"
],
"pattern": "^(.*)$"
},
"description": {
"$id": "#/properties/listexpressmail/items/properties/description",
"type": "string",
"title": "The Description Schema",
"default": "",
"examples": [
"投递并签收"
],
"pattern": "^(.*)$"
},
"orgfullname": {
"$id": "#/properties/listexpressmail/items/properties/orgfullname",
"type": "string",
"title": "The Orgfullname Schema",
"default": "",
"examples": [
"深圳市揽投部"
],
"pattern": "^(.*)$"
},
"properdelivery": {
"$id": "#/properties/listexpressmail/items/properties/properdelivery",
"type": "string",
"title": "The Properdelivery Schema",
"default": "",
"examples": [
"10"
],
"pattern": "^(.*)$"
},
"action": {
"$id": "#/properties/listexpressmail/items/properties/action",
"type": "string",
"title": "The Action Schema",
"default": "",
"examples": [
"10"
],
"pattern": "^(.*)$"
},
"serialnumber": {
"$id": "#/properties/listexpressmail/items/properties/serialnumber",
"type": "string",
"title": "The Serialnumber Schema",
"default": "",
"examples": [
"20170729092358"
],
"pattern": "^(.*)$"
}
}
}
}
}
}
- 在项目中导入需要用到的jar包(org.everit.json.schema-1.4.1.jar),jar包下载地址:http://central.maven.org/maven2/org/everit/json/org.everit.json.schema/1.4.1/
- 实现代码,若需校验的json数据与标准文件存在差异,e.getErrorMessage();可将差异进行打印
//这个就是你设定的标准JSON
InputStream inputStream = getClass().getResourceAsStream("/Schema.json");
//这个是你打算验证的JSON,这里我也用一份文件来存放,你也可以使用string或者jsonObject
InputStream inputStream1 = getClass().getResourceAsStream("/failure.json");
JSONObject Schema = new JSONObject(new JSONTokener(inputStream));
JSONObject data = new JSONObject(new JSONTokener(inputStream1));
Schema schema = SchemaLoader.load(Schema);
try {
schema.validate(data);
} catch (ValidationException e) {
System.out.println(e.getErrorMessage());
}
- 注意
此处引用的json为org.json,若使用了其他json包,需进行处理,否则SchemaLoader.load()方法异常。