如何显示对象的每个键都具有相同的类型?

时间:2018-08-09 19:28:58

标签: json jsonschema jsonschema2pojo

我有一个JSON响应,它返回各种指标作为我想要表示为JSON模式的值和置信度(以及通过使用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"
            }
        }
    }
}

但是如何针对一个对象的值呢?

1 个答案:

答案 0 :(得分:1)

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

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