在本页中,您将找到说明不同用例的示例,以帮助您充分利用 JSON 模式。这些示例涵盖了广泛的场景,每个示例都附带 JSON 数据和解释,展示了如何将 JSON 模式应用于各个领域。您可以修改这些示例以适合您的特定需求,因为这只是您可以利用 JSON 模式的众多方法之一。
地址
表示地址的 Schema,包含不同地址组件的可选属性,强制要求 locality
、region
和 countryName
,如果提供了 postOfficeBox
或 extendedAddress
,则还必须提供 streetAddress
。
Schema
{ "$id": "https://example.com/address.schema.json", "$schema": "https://json-schema.fullstack.org.cn/draft/2020-12/schema", "description": "An address similar to http://microformats.org/wiki/h-card", "type": "object", "properties": { "postOfficeBox": { "type": "string" }, "extendedAddress": { "type": "string" }, "streetAddress": { "type": "string" }, "locality": { "type": "string" }, "region": { "type": "string" }, "postalCode": { "type": "string" }, "countryName": { "type": "string" } }, "required": [ "locality", "region", "countryName" ], "dependentRequired": { "postOfficeBox": [ "streetAddress" ], "extendedAddress": [ "streetAddress" ] }}
数据
{ "postOfficeBox": "123", "streetAddress": "456 Main St",……
博客文章
表示博客文章的 Schema,包含诸如 title
(标题)、content
(内容)、publishedDate
(发布日期)、author
(作者)和 tags
(标签)之类的属性。
Schema
{ "$id": "https://example.com/blog-post.schema.json", "$schema": "https://json-schema.fullstack.org.cn/draft/2020-12/schema", "description": "A representation of a blog post", "type": "object", "required": ["title", "content", "author"], "properties": { "title": { "type": "string" }, "content": { "type": "string" }, "publishedDate": { "type": "string", "format": "date-time" }, "author": { "$ref": "https://example.com/user-profile.schema.json" }, "tags": { "type": "array", "items": { "type": "string" } } }}
数据
{ "title": "新的博客文章", "content": "这是博客文章的内容...", "publishedDate": "2023-08-25T15:00:00Z",
日历
一个表示日历事件的Schema,包含诸如startDate
、endDate
、summary
、location
和recurrenceDate
等详细信息的属性。geo
属性是对在不同位置定义的另一个Schema的引用($ref
),该Schema表示具有纬度和经度值的地理位置。
Schema
{ "$id": "https://example.com/calendar.schema.json", "$schema": "https://json-schema.fullstack.org.cn/draft/2020-12/schema", "description": "A representation of an event", "type": "object", "required": [ "dtstart", "summary" ], "properties": { "startDate": { "type": "string", "description": "Event starting time" }, "endDate": { "type": "string", "description": "Event ending time" }, "summary": { "type": "string" }, "location": { "type": "string" }, "url": { "type": "string" }, "duration": { "type": "string", "description": "Event duration" }, "recurrenceDate": { "type": "string", "description": "Recurrence date" }, "recurrenceDule": { "type": "string", "description": "Recurrence rule" }, "category": { "type": "string" }, "description": { "type": "string" }, "geo": { "$ref": "https://example.com/geographical-location.schema.json" } }}
数据
{ "startDate": "2023-08-25T10:00:00Z", "endDate": "2023-08-25T12:00:00Z", "summary": "Conference Presentation", "location": "Conference Center", "recurrenceDule": "FREQ=DAILY;COUNT=5"}
设备类型
此模式表示具有 deviceType
属性的电子设备,该属性决定了设备的类别,例如 smartphone
或 laptop
。它使用 oneOf
关键字根据 deviceType
属性动态引用模式。这种灵活的模式结构允许数据根据指定的 deviceType 符合相应的设备模式,从而可以轻松描述具有其独特特征的不同电子设备。当 deviceType
设置为 smartphone
时,模式会强制执行特定于智能手机的属性;当 deviceType
设置为 laptop
时,模式会强制执行特定于笔记本电脑的属性。
{ "$id": "https://example.com/device.schema.json", "$schema": "https://json-schema.fullstack.org.cn/draft/2020-12/schema", "type": "object", "properties": { "deviceType": { "type": "string" } }, "required": ["deviceType"], "oneOf": [ { "properties": { "deviceType": { "const": "smartphone" } }, "$ref": "https://example.com/smartphone.schema.json" }, { "properties": { "deviceType": { "const": "laptop" } }, "$ref": "https://example.com/laptop.schema.json" } ]}
{ "$id": "https://example.com/smartphone.schema.json", "$schema": "https://json-schema.fullstack.org.cn/draft/2020-12/schema", "type": "object", "properties": { "brand": { "type": "string" }, "model": { "type": "string" }, "screenSize": { "type": "number" } }, "required": ["brand", "model", "screenSize"]}
{ "$id": "https://example.com/laptop.schema.json", "$schema": "https://json-schema.fullstack.org.cn/draft/2020-12/schema", "type": "object", "properties": { "brand": { "type": "string" }, "model": { "type": "string" }, "processor": { "type": "string" }, "ramSize": { "type": "number" } }, "required": ["brand", "model", "processor", "ramSize"]}
数据
{ "deviceType": "smartphone", "brand": "Samsung", "model": "Galaxy S21", "screenSize": 6.2}
电子商务系统
这是一个表示电子商务系统的Schema,其中$anchor
用于product
和order
schema的定义中,以定义锚点:分别为ProductSchema
和OrderSchema
。
Schema
{ "$id": "https://example.com/ecommerce.schema.json", "$schema": "https://json-schema.fullstack.org.cn/draft/2020-12/schema", "$defs": { "product": { "$anchor": "ProductSchema", "type": "object", "properties": { "name": { "type": "string" }, "price": { "type": "number", "minimum": 0 } } }, "order": { "$anchor": "OrderSchema", "type": "object", "properties": { "orderId": { "type": "string" }, "items": { "type": "array", "items": { "$ref": "#ProductSchema" } } } } }}
数据
{ "order": { "orderId": "ORD123",...(其余代码省略,翻译方法相同)
地理位置
一个表示地理坐标的schema,包含latitude
(纬度)和longitude
(经度)值,这些值在指定范围内。
Schema
{ "$id": "https://example.com/geographical-location.schema.json", "$schema": "https://json-schema.fullstack.org.cn/draft/2020-12/schema", "title": "Longitude and Latitude Values", "description": "A geographical coordinate.", "required": [ "latitude", "longitude" ], "type": "object", "properties": { "latitude": { "type": "number", "minimum": -90, "maximum": 90 }, "longitude": { "type": "number", "minimum": -180, "maximum": 180 } }}
数据
{ "latitude": 48.858093, "longitude": 2.294694}
健康记录
表示健康记录的 schema,包括patientName
(患者姓名)、dateOfBirth
(出生日期)、bloodType
(血型)、allergies
(过敏史)、conditions
(病情)、medications
(用药)和emergencyContact
(紧急联系人)。
Schema
{ "$id": "https://example.com/health-record.schema.json", "$schema": "https://json-schema.fullstack.org.cn/draft/2020-12/schema", "description": "Schema for representing a health record", "type": "object", "required": ["patientName", "dateOfBirth", "bloodType"], "properties": { "patientName": { "type": "string" }, "dateOfBirth": { "type": "string", "format": "date" }, "bloodType": { "type": "string" }, "allergies": { "type": "array", "items": { "type": "string" } }, "conditions": { "type": "array", "items": { "type": "string" } }, "medications": { "type": "array", "items": { "type": "string" } }, "emergencyContact": { "$ref": "https://example.com/user-profile.schema.json" } }}
数据
{ "patientName": "Jane Doe", "dateOfBirth": "1985-02-15", "bloodType": "A+", "allergies": ["Pollen", "Penicillin"], "conditions": ["Hypertension", "Diabetes"], "medications": ["Lisinopril", "Metformin"], "emergencyContact": { "username": "emergencyuser", "email": "[email protected]" }} 招聘信息
表示招聘信息的 schema,包含title
(标题)、company
(公司)、location
(地点)、description
(描述)、employmentType
(雇佣类型)、salary
(薪资)和applicationDeadline
(申请截止日期)等属性。它还使用$anchor
关键字来定义锚点。
Schema
{ "$id": "https://example.com/job-posting.schema.json", "$schema": "https://json-schema.fullstack.org.cn/draft/2020-12/schema", "description": "A representation of a job posting", "type": "object", "required": ["title", "company", "location", "description"], "properties": { "title": { "type": "string" }, "company": { "type": "string" }, "location": { "type": "string" }, "description": { "type": "string" }, "employmentType": { "type": "string" }, "salary": { "type": "number", "minimum": 0 }, "applicationDeadline": { "type": "string", "format": "date" } }}
数据
{ "title": "软件工程师", "company": "科技解决方案有限公司", "location": "城市",以此类推...
电影
表示电影的 schema,包含诸如 title
(片名)、director
(导演)、release date
(发行日期)、genre
(类型)、duration
(时长)以及 cast members
(演员)等属性。
Schema
{ "$id": "https://example.com/movie.schema.json", "$schema": "https://json-schema.fullstack.org.cn/draft/2020-12/schema", "description": "A representation of a movie", "type": "object", "required": ["title", "director", "releaseDate"], "properties": { "title": { "type": "string" }, "director": { "type": "string" }, "releaseDate": { "type": "string", "format": "date" }, "genre": { "type": "string", "enum": ["Action", "Comedy", "Drama", "Science Fiction"] }, "duration": { "type": "string" }, "cast": { "type": "array", "items": { "type": "string" }, "additionalItems": false } }}
数据
{ "title": "示例电影", "director": "约翰·导演", "releaseDate": "2023-07-01", "genre": "动作", "duration": "2小时15分钟", "cast": ["演员A", "演员B", "演员C"]}
用户资料
表示用户配置文件的架构,包括诸如 username
(用户名)、email
(电子邮件)、fullName
(全名)、age
(年龄)、location
(位置)和 interests
(兴趣)等属性。
Schema
{ "$id": "https://example.com/user-profile.schema.json", "$schema": "https://json-schema.fullstack.org.cn/draft/2020-12/schema", "description": "A representation of a user profile", "type": "object", "required": ["username", "email"], "properties": { "username": { "type": "string" }, "email": { "type": "string", "format": "email" }, "fullName": { "type": "string" }, "age": { "type": "integer", "minimum": 0 }, "location": { "type": "string" }, "interests": { "type": "array", "items": { "type": "string" } } }}
数据