jsonSchema 的应用场景有很多,毕竟现在各个接口传输数据基本都是json,比如你做测试想对部分json字段进行校验或者统计你该如何写?解析json获取字段然后if else?
不是说不可以但是也太low了,完全可以用jsonSchema来解决这个问题。有人就会说了,可是我不会啊,不会写jsonSchema,也不知道怎么用。 没关系问题不大,几分钟就能搞定的问题
首先让我们取一个json 样例数据

{
"task_id": "f0f951a8-9883-11e9-a4ac-f0000aff48a4",
"success": 1000,
"result": {
"code": 1000,
"bad_cnt": 0,
"doubtList": [{
"phone": "",
"name": "贷款",
"key": "贷"
}],
"small_rate": 0.0,
"invalid_cnt": 11,
"phone_small": {
"": false,
"": true
}
}
}

然后打开网址 https://app.quicktype.io/#l=schema  输入json生成schema
如果你仔细观察一下的话你会发现,结构其实和你的json差不多是对应的,但是还不能直接用,下面我们只需要对照这个schema做一点修改就可以投入使用了

{
"$schema": "http://json-schema.org/draft-06/schema#",
"$ref": "#/definitions/Welcome",
"definitions": {
"Welcome": { # 名字随便起
"type": "object",
"additionalProperties": false, # 这里要改成true
"properties": {
"task_id": { #task_id 字段的相关验证
"type": "string", #数据类型 字符串
"format": "uuid" #数据格式uuid
},
"success": {
"type": "integer" #数据类型 int
},
"result": {
"$ref": "#/definitions/Result" # result 字段的相关验证对应下面的Result
}
},
"required": [ #表示需要校验的字段,比如我们只需要校验 'success' 可以把别的删除
"result",
"success",
"task_id"
],
"title": "Welcome"
},
"Result": { # 这是是result字段的校验信息
"type": "object",
"additionalProperties": false, # 改成true
"properties": {
"code": {
"type": "integer"
},
"bad_cnt": {
"type": "integer"
},
"doubtList": {
"type": "array",
"items": {
"$ref": "#/definitions/DoubtList"
}
},
"small_rate": {
"type": "integer"
},
"invalid_cnt": {
"type": "integer"
},
"phone_small": {
"type": "object",
"additionalProperties": {
"type": "boolean"
}
}
},
"required": [
"bad_cnt",
"code",
"doubtList",
"invalid_cnt",
"phone_small",
"small_rate"
],
"title": "Result"
},
"DoubtList": {
"type": "object",
"additionalProperties": false,
"properties": {
"phone": {
"type": "string"
},
"name": {
"type": "string"
},
"key": {
"type": "string"
}
},
"required": [
"key",
"name",
"phone"
],
"title": "DoubtList"
}
}
}

好,现在开始正式干活

a = {
"task_id": "f0f951a8-9883-11e9-a4ac-f0000aff48a4",
"success": 1000, #比如我们现在要检验这个字段必须是1000,具体字段条件咨询百度,
"result": {
"code": 1000,
"bad_cnt": 0,
"doubtList": [{
"phone": "",
"name": "贷款",
"key": "贷"
}],
"small_rate": 0.0,
"invalid_cnt": 11,
"phone_small": {
"": false,
"": true
}
}
}
#那我们修改schema
s = {
"$schema": "http://json-schema.org/draft-06/schema#",
"$ref": "#/definitions/Welcome",
"definitions": {
"Welcome": {
"type": "object",
"additionalProperties": True,
"properties": {
"success": {
"type": "integer",
"enum":[1000] #限定枚举值
}
},
"required": [
"success"
],
"title": "Welcome"
}
}
}
from jsonschema import validate
try:
validate(s,a)
print('ok')
except:
print('error')

学会了没有?没学会不要紧我们再来一次

a = {
"task_id": "f0f951a8-9883-11e9-a4ac-f0000aff48a4",
"success": 1000,
"result": {
"code": 1000,
"bad_cnt": 0,
"doubtList": [{
"phone": "",
"name": "贷款",
"key": "贷"
}],
"small_rate": 0.0, # 我们要验证这个字段是float 最小值是0
"invalid_cnt": 11,
"phone_small": {
"": false, #顺便验证这个字段是boolean,而且只能是True
"": true
}
}
}
那么schema应该这么改:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"$ref": "#/definitions/Welcome",
"definitions": {
"Welcome": {
"type": "object",
"additionalProperties": True,
"properties": {
"result": {
"$ref": "#/definitions/Result"
}
},
"required": [
"result"
],
"title": "Welcome"
},
"Result": {
"type": "object",
"additionalProperties": True,
"properties": {
"small_rate": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": True
},
"phone_small": {
"type": "object",
"additionalProperties": {
"type": "boolean",
"enum":[True]
}
}
},
"required": [
"phone_small",
"small_rate"
],
"title": "Result"
}
}
}

OK轻松加愉快,照葫芦画瓢就行了。什么?你还想知道别的字段限制条件?有肯定是有的,但是我不会说,因为说了你也记不住,具体用的时候自行百度。

最新文章

  1. SQLMAP 中$与#的区别
  2. C 语言学习的第 04 课:编译器常见错误和警告(1)
  3. 0525Scrum项目7.0
  4. Apache Spark源码走读之12 -- Hive on Spark运行环境搭建
  5. demo16Toast
  6. R语言实战
  7. DEEPIN下搭建FTP服务器步骤(备忘录)
  8. stringUtils是apache下的Java jar补充包
  9. Java [Leetcode 235]Lowest Common Ancestor of a Binary Search Tree
  10. DML_数据操纵语言
  11. 1065. A+B and C (64bit)
  12. java如何防止反编译
  13. [转]MD5加密算法的java实现
  14. Spring框架入门
  15. 去除CSDN 博客页广告的历程
  16. Android水印相机
  17. 磊哥测评之数据库SaaS篇:腾讯云控制台、DMC和小程序
  18. 在Mac OS X下使用Apache、PHP、MySQL、Netbeans、Yii
  19. HDFS配置参数及优化之实战经验(Linux hdfs)
  20. spring mvc 映射与适配器

热门文章

  1. java并发编程之美-阅读记录4
  2. 49.求1+2+3+.......+n
  3. js转换成数字
  4. java.nio.Buffer 中的 flip()方法
  5. python模块打补丁
  6. h5py库
  7. python 在图像上写中文字体 (python write Chinese in image)
  8. php图片无损压缩的问题解决
  9. Web源码泄露总结
  10. mysql数据库帐号权限设置