zoukankan      html  css  js  c++  java
  • json schema校验

    工作中使用到了json schema格式校验的问题,在网上查了些资料,结合自己的理解记录一下。

    json schema可以对json结果字符串做出一些约束,例如:

    1. 值类型是:array, object, number, string等等

    2.值类型必须是枚举中的一个

    3. 字符串的长度限制

    4. 对字符串判断是否符合正则表达式

    5. array元素个数

    6. object对象必要属性

    测试schema文件

     1 {
     2     "$schema": "http://json-schema.org/draft-04/schema#",
     3     "title": "Product set",
     4     "type": "array",
     5     "items": {
     6         "title": "Product",
     7         "type": "object",
     8         "properties": {
     9             "name": {
    10                 "type": "string"
    11             },
    12             "price": {
    13                 "type": "number",
    14                 "minimum": 0,
    15                 "maximum":30,
    16                 "exclusiveMinimum": true
    17             },
    18             "tags": {
    19                 "type": "array",
    20                 "items": {
    21                     "type": "string",
    22                     "minLength":10,
    23                     "maxLength":1024,
    24                     "pattern":"http://.*"
    25                 },
    26                 "minItems": 2,
    27                 "uniqueItems": true
    28             },
    29             "city":{
    30               "type":"string",
    31               "enum" : ["bj", "sh", "hk"]
    32             }
    33         },
    34         "required": ["name", "price"]
    35     }
    36 }

    测试json

     1 [
     2   {
     3     "name":"san",
     4     "price":29,
     5     "tags":["http://sdtr/sdg", "http://qwewret/qsdf"],
     6     "city":"bj"
     7   },
     8   {
     9     "name":"dong",
    10     "price":30,
    11     "tags":["http://test", "http://sina"],
    12     "city":"hk"
    13   },
    14 ]

    参考:

    http://blog.csdn.net/Miss_Ashelley/article/details/53285762

    http://www.jsonschemavalidator.net/

    http://json-schema.org/

  • 相关阅读:
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    【转】idea 用maven骨架生成项目速度慢的问题
  • 原文地址:https://www.cnblogs.com/xudong-bupt/p/7355853.html
Copyright © 2011-2022 走看看