【问题标题】:How to show that each key of an object will have the same type?如何表明对象的每个键都具有相同的类型?
【发布时间】:2019-01-17 08:39:38
【问题描述】:

我有一个 JSON 响应,它返回各种指标作为我希望表示为 JSON Schema 的值和置信度(以及使用 JsonSchema2Pojo 生成 bean)。

{
    "QPI": {
        "value": 0.053916827852998075,
        "confidence": 0.89127
    },
    "MTBF": {
        "value": 0.053916827852998075,
        "confidence": 0.90210
    },
    "MDT": {
        "value": 0.053916827852998075,
        "confidence": 0.63541
    }
}

响应中的指标数量不是固定的,因此我无法将它们表示为属性。

如果响应是

[
    {
        "metric": "QPI",
        "value": 0.053916827852998075,
        "confidence": 0.89127
    },
    {
        "metric": "MTBF",
        "value": 0.053916827852998075,
        "confidence": 0.90210
    }, 

    {
        "metric": "MDT",
        "value": 0.053916827852998075,
        "confidence": 0.63541
    }
]

然后我可以写一个像

这样的模式
{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "metric": {
                "type": "string"
            },
            "value": {
                "type": "number"
            },
            "confidence": {
                "type": "number"
            }
        }
    }
}

但是如何为对象的值做呢?

【问题讨论】:

    标签: json jsonschema jsonschema2pojo


    【解决方案1】:

    "additionalProperties" 不仅像 "additionalProperties": false 那样是布尔值,还可以采用预期的对象类型:

    {
        "type": "object",
        "additionalProperties": {
            "type": "object",
            "properties": {
                "value": {
                    "type": "number"
                },
                "confidence": {
                    "type": "number"
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 2016-01-18
      • 2018-12-30
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多