相关疑难解决方法(0)

如何在JSON模式中使用定义(draft-04)

我正在使用的其余服务响应类似于以下示例,我在此处仅包含3个字段,但还有更多:

{
    "results": [
        {
            "type": "Person",
            "name": "Mr Bean",
            "dateOfBirth": "14 Dec 1981"
        },
        {
            "type": "Company",
            "name": "Pi",
            "tradingName": "Pi Engineering Limited"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我想为上面的(draft-04)编写一个JSON模式文件,它将明确指定:

if type == Person then list of required properties is ["type", "name", "dateOfBirth", etc] 
OR
if type == "Company" then list of required properties is ["type", "name", "tradingName", etc]
Run Code Online (Sandbox Code Playgroud)

但是我无法找到任何文档或如何执行此操作的示例.

目前我的JSON架构如下所示:

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "type": "object",
    "required": ["results" ],
    "properties": {
        "results": {
            "type": "array",
            "items": {
                "type": "object",
                "required": ["type", "name"], …
Run Code Online (Sandbox Code Playgroud)

json jsonschema

20
推荐指数
2
解决办法
2万
查看次数

如何指定 JSON 实例由特定的 JSON Schema 定义

我的问题是如何知道使用哪个 JSON 模式来验证特定的 JSON 文档?我在 id 字段中指定了模式的 URL。这够了吗?我应该把 id 放在 JSON 中吗?我不确定我是否了解如何将 JSON 连接到特定的 JSON 架构。

这是我的架构

{
 "$schema": "http://json-schema.org/draft-04/schema#",
 "id": "url/schema.json",
 "title": "title",
 "definitions": {
    "emailObject": {
        "type": "object",
        "properties":{
            "name": {
                "description": "The name of the customer",
                "type": "string",
                "maxLength": 200
            },
            "email": {
                "description": "The email of the customer",
                "type": "string",
                "format": "email",
                "maxLength": 100
            }
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)

java json jsonschema

5
推荐指数
1
解决办法
674
查看次数

标签 统计

json ×2

jsonschema ×2

java ×1