问题
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