![](/img/trans.png)
[英]How to specify a different name for json key while using Retrofit + Moshi
[英]How to specify the key in openAPI JSON whose name is not static
我想創建一個具有 openAPI 規范的 JSON。 api/endpoint
的示例負載格式如下,
"abc": {"name": "PA", "id": "21"}
所以這里是 openapi JSON 格式,
{
"openapi": "3.0.0",
"info": {...},
"paths": {
"api/endpoint": {
"put": {
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/putSchema"
}
}
}
}
}
}
},
"components": {
"schemas": {
"putSchema": {
"abc": { ----------> *
"type": "object",
"properties": {
"name": {...},
"id": {...}
}
}
}
}
}
}
但我的有效負載可能會有所不同,例如,
"abc": {"name": "PA", "id": "21"}
"xyz": {"name": "ST", "id": "35"}
"def": {"name": "UV", "id": "94"}
不確定如何為動態密鑰名稱定義 JSON 格式(此處為 abc/xyz/def)-(即)在 JSON 中的 * 處填寫什么)。
所以我的查詢是如何在JSON中指定名稱不是static的密鑰。
在您的示例中, putSchema
是一個字符串到對象 map,其中“abc”/“xyz”/etc。 是 map 中的鍵。使用additionalProperties
關鍵字定義映射:
"putSchema": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"name": { ...},
"id": { ... }
}
}
}
如果您的負載只有一個根密鑰(例如,只有“abc”但不能同時使用“abc”和“xyz”),您可以將"maxProperties": 1
添加到您的putSchema
以限制根密鑰的數量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.