Как определить схему JSON для карты<String, Integer>?

У меня есть JSON:

{
"itemType": {"food":22,"electrical":2},
"itemCount":{"NA":211}
}

Здесь itemType и itemCount будут общими, но не значения внутри них (еда, NA, электричество), которые будут постоянно меняться, но будут в формате: Карта

Как определить схему Json для такой общей структуры?

Я старался:

"itemCount":{
      "type": "object"
    "additionalProperties": {"string", "integer"}

    }

4 ответа

Решение

Вы можете:

{
  "type": "object",
  "properties": {
    "itemType": {"$ref": "#/definitions/mapInt"},
    "itemCount": {"$ref": "#/definitions/mapInt"}
  },
  "definitions": {
    "mapInt": {
      "type": "object",
      "additionalProperties": {"type": "integer"}
    }
  }
}

Вопрос плохо описан, позвольте мне посмотреть, смогу ли я перефразировать его, а также ответить на него.

Вопрос: Как представить карту в схеме json следующим образом Map<String, Something>

Отвечать:

Похоже, вы можете использовать Additional Propertiesчтобы выразить это https://json-schema.org/understanding-json-schema/reference/object.html#additional-properties

      {
  "type": "object",
  "additionalProperties": { "type": "something" }
}

Например, допустим, вы хотите Map<string, string>

      {
  "type": "object",
  "additionalProperties": { "type": "string" }
}

Или что-то более сложное, например Map<string, SomeStruct>

      {
  "type": "object",
  "additionalProperties": { 
    "type": "object",
    "properties": {
      "name": "stack overflow"
    }
  }
}
      "properties": {
  "myFieldName": {
    "existingJavaType" : "java.util.Map<String,String>",
    "type" : "object"
  }
}

ссылка: https://www.jsonschema2pojo.org/

{
    "type": "array",
    "maxItems": 125,
    "items": {
        "type": "array",
        "items": [
            { // key schema goes here },
            { // value schema goes here }
        ],
        "additionalItems": false
    }
}
Другие вопросы по тегам