JSON模式是基于JSON格式定义JSON数据结构的规范。它被写在IETF草案,于2011年到期。 JSON模式:
描述现有的数据格式
干净的人类和机器可读的文档
完成结构验证,可用于自动化测试
完成结构验证,验证客户端提交的数据
JSON模式验证库
有几个验证器目前可用于不同的编程语言。目前最完整和最兼容的JSON模式验证可用JSV
JSON模式示例
以下是一个基本的JSON模式,其中涵盖了经典的产品目录说明:
{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "Product", "description": "A product from Acme's catalog", "type": "object", "properties": { "id": { "description": "The unique identifier for a product", "type": "integer" }, "name": { "description": "Name of the product", "type": "string" }, "price": { "type": "number", "minimum": 0, "exclusiveMinimum": true } }, "required": ["id", "name", "price"] }
让我们来看看在这个模式中可以使用的各种重要的关键词:
也可以同时查阅 http://json-schema.org 的关键字可以用在定义JSON模式的完整列表。以上模式可用于测试的有效性,下面给出的JSON代码:
[ { "id": 2, "name": "An ice sculpture", "price": 12.50, }, { "id": 3, "name": "A blue mouse", "price": 25.50, } ]