亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Json-schema簡介和應用

Json?

了解json schema首先要知道什么是json?
json 是 JavaScript Object Notation 的缩写,它是一种简化的数据交换格式,是目前互联网服务间进行数据交换最常见的一种交换格式,具有简洁、可读性好等特点。

在json中常见的数据类型主要包括

  • object

    { "key1": "value1", "key2": "value2" }

  • array

    [ "first", "second", "third" ]

  • number

    42
    3.1415926

  • string

    "This is a string"

  • boolean

    true
    false

  • null

    null

一个示例json格式比如:

{
  "fruits": [ "apple", "orange", "pear" ],
  "vegetables": [
    {
      "veggieName": "potato",
      "veggieLike": true
    },
    {
      "veggieName": "broccoli",
      "veggieLike": false
    }
  ]
}
何为Json Schema

如前文所述,json是目前应用非常广泛的数据狡猾格式。既然是用于数据交换的格式,那么就存在数据交换的是双方,如何约定或校验对方的数据格式符合要求,就成了服务交互需要解决的一个问题。所以Json Schema就是用来定义json数据约束的一个标准。根据这个约定模式,交换数据的双方可以理解json数据的要求和约束,也可以据此对数据进行验证,保证数据交换的正确性。

目前最新的Json-schema版本是draft 7,发布于2018-03-19。下面我们就以官网的一个实例来看看Json-schema是如何进行数据约束以及其应用

如下是一个schema实例:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.com/product.schema.json",
  "title": "Product",
  "description": "A product from Acme's catalog",
  "type": "object",
  "properties": {
    "productId": {
      "description": "The unique identifier for a product",
      "type": "integer"
    },
    "productName": {
      "description": "Name of the product",
      "type": "string"
    },
    "price": {
      "description": "The price of the product",
      "type": "number",
      "exclusiveMinimum": 0
    },
    "tags": {
      "description": "Tags for the product",
      "type": "array",
      "items": {
        "type": "string"
      },
      "minItems": 1,
      "uniqueItems": true
    },
    "dimensions": {
      "type": "object",
      "properties": {
        "length": {
          "type": "number"
        },
        "width": {
          "type": "number"
        },
        "height": {
          "type": "number"
        }
      },
      "required": [ "length", "width", "height" ]
    }
  },
  "required": [ "productId", "productName", "price" ]
}

分析说明:

  "$schema": "http://json-schema.org/draft-07/schema#",

说明当前使用的schema版本,可以不包含

  "$id": "http://example.com/product.schema.json",

当前schema的唯一id标识,一般指向一个自主域名。方便后续引用,可以不包含

  "title": "Product",

当前schema的标题,简要描述信息,可不包含

"description": "A product from Acme's catalog",

详细描述信息,可不包含

"type": "object",

约束对象是object,也就是在 { } 中的数据

"properties": {
    "productId": {
      "description": "The unique identifier for a product",
      "type": "integer"
    },

object中具体属性的约束,description是描述信息,不产生具体约束。
type约束productid属性类型为整型

 "productName": {
      "description": "Name of the product",
      "type": "string"
    },

约束productName属性类型为字符型

    "price": {
      "description": "The price of the product",
      "type": "number",
      "exclusiveMinimum": 0
    },

约束price属性类型为数字型,可以是整型或浮点型。
exclusiveMinimum约束该数字>0(不包含0)

    "tags": {
      "description": "Tags for the product",
      "type": "array",
      "items": {
        "type": "string"
      },
      "minItems": 1,
      "uniqueItems": true
    },

约束tag属性是array数组。items是数组项约束,这里约束数组项均为字符型
minItems数组至少包含1项。
uniqueItems约束数组中每项不得重复

    "dimensions": {
      "type": "object",
      "properties": {
        "length": {
          "type": "number"
        },
        "width": {
          "type": "number"
        },
        "height": {
          "type": "number"
        }
      },
  "required": [ "length", "width", "height" ]
    }
  },

约束dimensions嵌套对象,其中length,width,height均为数字类型
且这三个字段在dimensions对象中必须包含

 "required": [ "productId", "productName", "price" ]

当前数据对象必须包含productId,productName,price三个字段

以上是对Json-Schema的简要说明,详细介绍大家可以关注后续更新和相关实战课。

點擊查看更多內容
9人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
軟件測試工程師
手記
粉絲
1.5萬
獲贊與收藏
502

關注作者,訂閱最新文章

閱讀免費教程

感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消