zoukankan      html  css  js  c++  java
  • JsonSchema 启蒙

    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": "13366179991",
                "name": "贷款",
                "key": ""
            }],
            "small_rate": 0.0,
            "invalid_cnt": 11,
            "phone_small": {
                "18611516150": false,
                "13240350826": 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": "13366179991",
                "name": "贷款",
                "key": ""
            }],
            "small_rate": 0.0,
            "invalid_cnt": 11,
            "phone_small": {
                "18611516150": false,
                "13240350826": 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": "13366179991",
                "name": "贷款",
                "key": ""
            }],
            "small_rate": 0.0,  # 我们要验证这个字段是float 最小值是0
            "invalid_cnt": 11,
            "phone_small": {
                "18611516150": false, #顺便验证这个字段是boolean,而且只能是True
                "13240350826": 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轻松加愉快,照葫芦画瓢就行了。什么?你还想知道别的字段限制条件?有肯定是有的,但是我不会说,因为说了你也记不住,具体用的时候自行百度。

  • 相关阅读:
    osgearth cache
    3ds Max导出FBX动画模型在OSG中使用
    osgExp只能将3dmax中的动画导出为路径动画osg::AnimationPath,而不能导出osgAnimation::Animation。osg播放骨骼动画应该使用FBX格式
    ExtJS前端框架EXT弹出窗口事件
    大数据学习——securecrt同时向多个tab窗口发送相同的命令
    大数据学习——yarn集群启动
    如何取SQL结果集的第一条记录
    Java比较两个数组中的元素是否相同的最简单方法
    大数据学习——hdfs集群启动
    大数据学习——hadoop安装
  • 原文地址:https://www.cnblogs.com/tigerzhouv587/p/11239607.html
Copyright © 2011-2022 走看看