JSON data enum types

一笑奈何 提交于 2021-02-08 09:18:17

问题


I have a JSON object like this.

var data={
"Company" : "XYZ",
"company" : ['RX','TX']
}

The above json data has 2 keys Company whose type is string and company whose type is enum but not array(I didnt know how to represent enum in json data),because of which json schema says its an array. I want it to be enum type.

So how will I represent Enum type in JSON data?


回答1:


JSON has no enum type. The two ways of modeling an enum would be:

An array, as you have currently. The array values are the elements, and the element identifiers would be represented by the array indexes of the values. This, however, does not model sparse enums (enums where the first index is not zero OR where the identifiers are not sequential).

enum suit {
  clubs = 0,
  diamonds,
  hearts,
  spades,
};

// is equivalent to

"suitEnum": ["clubs", "diamonds", "hearts", "spades"];

A map, which is less compact but solves the array limitations:

enum suit {
  clubs = 10,
  diamonds = 20,
  hearts = 30,
  spades = 40,
};

// is equivalent to

"suitEnum": {
  "clubs": 10,
  "diamonds": 20,
  "hearts": 30,
  "spades" 40,
};


来源:https://stackoverflow.com/questions/45052750/json-data-enum-types

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!