JSON Schema校验Json数据字段

本文介绍如何使用JSONSchema校验JSON数据的合规性,包括创建校验文件、使用工具自动生成校验标准及实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自使用JSON Schema校验JSON数据是否合规,感谢博主的奉献!

  1. 用处
    通过JSON Schema可按照规定json数据样式对json数据进行校验,可校验的包括json的键是否符合要求,值是否存在或格式是否符合要求等,具体格式可点击查看Json schema 格式
  2. 创建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": "^(.*)$"
           }
         }
       }
     }
   }
 }
  1. 在项目中导入需要用到的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/
  2. 实现代码,若需校验的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());
}
  1. 注意
    此处引用的json为org.json,若使用了其他json包,需进行处理,否则SchemaLoader.load()方法异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值