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](https://github.com/garycourt/JSV) |
| Ruby JSON Schema Validator | Ruby | [https://github.com/hoxworth/json-schema](https://github.com/hoxworth/json-schema) |
| json-schema-validator | Java | [https://github.com/fge/json-schema-validator](https://github.com/fge/json-schema-validator) |
| php-json-schema (by MIT) | PHP | [https://github.com/hasbridge/php-json-schema](https://github.com/hasbridge/php-json-schema) |
| JSON.Net | .NET | [http://james.newtonking.com/projects/json-net.aspx](http://james.newtonking.com/projects/json-net.aspx) |
除了基于特定語言的模式驗證工具,有一個非常棒的在線JSON Schema驗證器:http://json-schema-validator.herokuapp.com。要使用該網站,只需輸入JSON文件和Schema到相應的文本框中,然后按驗證按鈕即可。