0

Core Json – JSON Schema

Posted by 撒得一地 on 2016年4月20日 in JSON

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

                        }

                    }

                }

            }

        }

    }

}

}

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到相应的文本框中,然后按验证按钮即可。

上一篇:

下一篇:

相关推荐

发表评论

电子邮件地址不会被公开。 必填项已用*标注

5 + 8 = ?

网站地图|XML地图

Copyright © 2015-2024 技术拉近你我! All rights reserved.
闽ICP备15015576号-1 版权所有©psz.