繁体   English   中英

创建JSON模式的层次结构

[英]Creating a hierarchy of JSON schemas

我正在使用来自不同类型的源的信息来构造一些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"]

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

“properties”:{“id”:1234,“name”:“源名称”,“description”:“源名称”,“类型”:“源类型”,“url”:“URL来源”,
“morePropertiesHere”:{“description”:“其余属性因源类型而异”}}如果Id是数字,那么您可以按原样提供它们而不是放入“”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM