牛骨文教育服务平台(让学习变的简单)

JSON Schema指定JSON文件的结构。 JSON Schema可以用于验证发送/接受于RESTful Web服务的内容。 JSON Schema都写在JSON里。

http://json-schema.org可以找到主要的JSON Schema。JSON Schema是一个正在发展的Schema- JSON的架构团队刚刚发布0.4版本,在http://tools.ietf.org/html/draft-zyp-json-schema-04. 可以找到跟多细节信息。 一些重要的JSON Schema结构包括:

CONSTRUCT DESCRIPTION
type The data type – object, array, string, number, etc.
$schema The URI that provides the schema version.
required true/false
id Data element id
properties Validation properties for a data element include type (see above), minimum – minimum value, maximum – maximum value, enum, etc.

下面的例子用一个简单的JSON Schema验证网上礼品登记信息的一部分内容:

{

"type": "object",

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

"id": "#",

"required": true,

"properties": {

    "registrants": {

        "type": "array",

        "id": "registrants",

        "required": true,

        "items": {

            "type": "object",

            "required": false,

            "properties": {

                "address": {

                    "type": "object",

                    "id": "address",

                    "required": true,

                    "properties": {

                        "city": {

                            "type": "string",

                            "id": "city",

                            "required": true

                        },

                        "country": {

                            "type": "string",

                            "id": "country",

                            "required": false

                        },

                        "line1": {

                            "type": "string",

                            "id": "line1",

                            "required": true

                        },

                        "line2": {

                            "type": "string",

                            "id": "line2",

                            "required": false

                        },

                        "postalCode": {

                            "type": "string",

                            "id": "postalCode",

                            "required": true

                        },

                        "premise": {

                            "type": "string",

                            "id": "premise",

                            "required": true,

                            "enum": [

                                "work",

                                "home",

                                "other"

                            ]

                        },

                        "stateOrProvince": {

                            "type": "string",

                            "id": "stateOrProvince",

                            "required": true

                        }

                    }

                },

                "firstName": {

                    "type": "string",

                    "id": "firstName",

                    "required": true

                },

                "lastName": {

                    "type": "string",

                    "id": "lastName",

                    "required": true

                },

                "phoneNumber": {

                    "type": "object",

                    "id": "phoneNumber",

                    "required": true,

                    "properties": {

                        "channel": {

                            "type": "string",

                            "id": "channel",

                            "required": true,

                            "enum": [

                                "cell",

                                "work",

                                "home"

                            ]

                        },

                        "number": {

                            "type": "string",

                            "id": "number",

                            "required": true

                        }

                    }

                }

            }

        }

    }

}

}

上面的schema:

  • 需要注册对象的数组。
  • 限制电话号码字段为下列值:手机,工作电话,传真或者和家用电话
  • 限制地址字段为:家庭地址,工作地址,或其他。

一个Web服务的消费者可以使用这个模式来验证下面的JSON文件:

{

"registrants": [

    {

        "firstName": "Fred",

        "lastName": "Smith",

        "phoneNumber": {

            "channel": "cell",

            "number": "303-555-1212"

        },

        "address": {

            "premise": "home",

            "line1": "555 Broadway NW",

            "line2": "# 000",

            "city": "Denver",

            "stateOrProvince": "CO",

            "postalCode": "88888",

            "country": "USA"

        }

    }

]

}

JSON Schema生成器

创建一个JSON Schema非常的繁琐,而且容易出错的。使用JSON Schema生成器,可以生成任何有效JSON文件的Schema。访问在线JSON模式发生器(www.jsonschema.net/),并通过执行以下操作生成模式:

  • 粘贴JSON文件到右边的文本区域。
  • 选择JSON输入选项。
  • 按生成模式按钮。

JSON Schema 验证器

应用程序使用JSON Schema验证器,以确保JSON文件符合Schema指定的结构。 JSON Schema验证器可用于大多数现代编程语言中:

JSON SCHEMA VALIDATOR LANGUAGE SOURCE
JSV JavaScript https://github.com/garycourt/JSV
Ruby JSON Schema Validator Ruby https://github.com/hoxworth/json-schema
json-schema-validator Java https://github.com/fge/json-schema-validator
php-json-schema (by MIT) PHP https://github.com/hasbridge/php-json-schema
JSON.Net .NET http://james.newtonking.com/projects/json-net.aspx

除了基于特定语言的模式验证工具,有一个非常棒的在线JSON Schema验证器:http://json-schema-validator.herokuapp.com。要使用该网站,只需输入JSON文件和Schema到相应的文本框中,然后按验证按钮即可。