gpt4 book ai didi

jsonschema - JSON 模式类型

转载 作者:行者123 更新时间:2023-12-02 05:23:46 24 4
gpt4 key购买 nike

我想使用 JSON Schema 并允许新类型(目前有字符串数字数组对象 boolean 和 null)

例如我想添加一个联系人类型

在 JSON 中甚至可能吗?有什么建议吗?

谢谢

最佳答案

如果您的目标是一次定义 contact 并多次重复使用它,您可以为名为 contactobject 创建一个定义然后在需要时使用 JSON 指针引用它。

其中的一个示例可能如下所示:

{
"type": "object",
"definitions": {
"contact": {
"properties": {
"name": {
"type": "string"
},
"phone": {
"type": "string"
}
}
}
},
"properties": {
"Person": {
"properties": {
"contact": {
"$ref" : "#/definitions/contact"
},
"birthday": {
"type": "string",
"format": "date-time"
}
}
},
"Company": {
"properties": {
"contact": {
"$ref" : "#/definitions/contact"
},
"inBusinessSince" : {
"type": "string",
"format": "date-time"
}
}
}
}
}

现在假设您希望在 Company 的联系人属性上拥有比在 Person 上更多的属性。您可以使用 allOf 关键字,这样上面的公司现在看起来像这样:

"Company": {
"properties": {
"contact": {
"allOf": [
{"$ref" : "#/definitions/contact"},
{
"properties": {
"fax": {
"type": "string"
}
}
],
"inBusinessSince" : {
"type": "string",
"format": "date-time"
}
}
}

有关更多信息,请参阅以下链接。

关于jsonschema - JSON 模式类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13453631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com