gpt4 book ai didi

json - 类似字典的 JSON 模式

转载 作者:行者123 更新时间:2023-12-03 12:35:00 25 4
gpt4 key购买 nike

我有一个 json 对象,它可以包含任意数量的具有特定规范的嵌套对象,例如:

{
"Bob": {
"age": "42",
"gender": "male"
},
"Alice": {
"age": "37",
"gender": "female"
}
}

并希望有一个类似于以下内容的架构:
{
"type": "object",
"propertySchema": {
"type": "object",
"required": [
"age",
"gender"
],
"properties": {
"age": {
"type": "string"
},
"gender": {
"type": "string"
}
}
}
}

我知道我可以将其转换为数组并将“名称”插入对象中。在这种情况下,我的架构看起来像:
{
"type": "array",
"items": {
"type": "object",
"required": [
"name",
"age",
"gender"
],
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "string"
},
"gender": {
"type": "string"
}
}
}
}

但我想要一个类似字典的结构。是否有可能制作这样的模式?

最佳答案

additionalProperties 是您的关键字:

{
"type" : "object",
"additionalProperties" : {
"type" : "object",
"required" : [
"age",
"gender"
],
"properties" : {
"age" : {
"type" : "string"
},
"gender" : {
"type" : "string"
}
}
}
}
additionalProperties可以具有以下不同含义的值:
  • "additionalProperties": false根本不允许更多的属性。
  • "additionalProperties": true允许更多的属性。这是默认行为。
  • "additionalProperties": {"type": "string"}允许附加属性(任意名称),如果它们具有给定类型的值(此处为“字符串”)。
  • "additionalProperties": {*any schema*}其他属性必须满足提供的架构,例如上面提供的示例。
  • 关于json - 类似字典的 JSON 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27357861/

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