zoukankan      html  css  js  c++  java
  • JSON Schema

     JSON Schema可以描述json文件,对属性类型及值范围进行约束,可以根据这些约束条件生成数据;

    如果json结构有多级嵌套,可以通过object和array组合进行描述,type 为 object 类型时,properties 关键字是必需的,当 type 为 array 类型时,items 关键字是必需的。object 和 array 类型的引入使得数据结构可以支持无限级嵌套。

    type:属性类型 string、number、null、integer、boolean、object、array。

    title和description:描述作用,不具有约束性。

    $schema:声明文档类型为JSON Schema文档,不是必须项。

    id:标识文档的唯一性,在文档范围内唯一,不允许重复。

    minimum/maximum:最小/最大属性约束值。

    maxItems/minItems:最大/最小item限制

    required:必须的属性。

    exclusiveMinimum/exclusiveMaximum:为“true”时执行最小/最大约束(如果有约束)。

    multipleOf:

    minLength/maxLength:字符串最小/最大字符数。

    pattern:如果正则匹配成功,则string字符串被认为有效。

    相关在线工具网址:

    http://json-schema-validator.herokuapp.com/  提供schema语法检查、与json的比对结果、java-schema、schema-java等工具。

    http://jeremydorn.com  提供根据schema和json生成web控件,可以在生成的控件上直接调整属性。

    例子:

    json schema:

     1 {
     2   "$schema": "http://json-schema.org/",
     3   "title":"test",
     4   "type":"object",
     5   "properties":{
     6     "meta":{
     7         "type":"object",
     8         "properties":{
     9             "code":{
    10             "type":"number"
    11             },
    12             "error":{
    13                 "type":"string"
    14             },
    15             "info":{
    16                 "type":"string"
    17             }
    18         }
    19     },
    20     "data":{
    21         "type":"object",
    22         "properties":{
    23             "id":{
    24             "type":"string"
    25             },
    26             "sipTellAddress":{
    27                 "type":"string"
    28             },
    29             "userlevel":{
    30                 "type":"number"
    31             },
    32             "username":{
    33                 "type":"string"
    34             },
    35             "authToken":{
    36                 "type":"string"
    37             },
    38             "lastOperateTime":{
    39                 "type":"string"
    40             },
    41             "ssId":{
    42                 "type":"null"
    43             },
    44             "sipPassword":{
    45                 "type":"string"
    46             },
    47             "mmpUser":{
    48                 "type":"string"
    49             },
    50             "mmpPassword":{
    51                 "type":"string"
    52             }
    53         }
    54     }    
    55   } 
    56 }

    json:

     1 {
     2   "meta": {
     3     "code": 200,
     4     "error": "",
     5     "info": ""
     6   },
     7   "data": {
     8     "id": "2D91A809-74FC-4DDF-9BBA-815709EB75EF",
     9     "sipTellAddress": "20070",
    10     "userlevel": 0,
    11     "username": "系统管理员",
    12     "authToken": "21f2ebc4-1025-456b-b601-ac2b30d6e628",
    13     "lastOperateTime": "2017-04-26 13:23:23",
    14     "ssId": null,
    15     "sipPassword": "winsion123",
    16     "mmpUser": "test-1208",
    17     "mmpPassword": "Winsion123"
    18   }
    19 }
  • 相关阅读:
    Solution: Win 10 和 Ubuntu 16.04 LTS双系统, Win 10 不能从grub启动
    在Ubuntu上如何往fcitx里添加输入法
    LaTeX 笔记---Q&A
    Hong Kong Regional Online Preliminary 2016 C. Classrooms
    Codeforces 711E ZS and The Birthday Paradox
    poj 2342 anniversary party
    poj 1088 滑雪
    poj 2479 maximum sum
    poj 2481 cows
    poj 2352 stars
  • 原文地址:https://www.cnblogs.com/test-researcher/p/6768344.html
Copyright © 2011-2022 走看看