创建JSON模式的层次结构

时间:2017-06-16 08:37:32

标签: json

我正在使用来自不同类型的来源的信息来构建一些JSON数据。我只关注一种类型的源文档,我收集并准备好了一些数据点。所以我决定尝试为有关文档的数据编写JSON模式。

无论源的类型如何,前四个属性(id,name,type和url)都必须存在,但每个属性中包含的其他信息会有所不同。我想将所有这些存储在同一个数据库中。我想在以后为每个其他类型的源编写一个模式会很有意义。

我很擅长使用JSON,目前的难点在于理解如何将这些模式整合在一起。什么是最佳做法?是否可以使用包含以下信息的顶级模式和每种类型的二级模式创建这些不同对象类型的层次结构?或者,更好地制作一个包含所有不同类型的源中可能存在的所有信息的更大的模式?

"$schema": "http://json-schema.org/schema#",
"title": "Document",
"description": "Information connected to a document",
"type": "object",
"properties": {
    "id": {
        "description": "The unique identifier",
        "type": "number"
    },
    "name": {
        "description": "Title of the source",
        "type": "string"
    },
    "type": {
        "description": "Type of source",
        "type": "string"
    },
    "url": {
        "description": "URL of the source",
        "type": "string"
    }, 
    "morePropertiesHere": {
        "description": "the rest of the properties vary depending on type of source"
    }
},
"required": ["id", "name", "type", "url"]

1 个答案:

答案 0 :(得分:0)

您可以像这样更改JSON中的属性部分,因为它只需要数字和文本。

" properties":{     " ID":1234,     " name":"来源名称",     "说明":"来源标题",     "输入":"来源类型",     " url":"来源的网址",
    " morePropertiesHere":{         "描述":"其余属性根据来源类型而变化"     } } 如果Id是数字,那么您可以按原样提供,而不是放入""。

相关问题