JSON数据枚举类型 [英] JSON data enum types

查看:644
本文介绍了JSON数据枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的JSON对象.

I have a JSON object like this.

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

上面的json数据有2个键,公司的类型为字符串,公司的类型为枚举,但不是数组(我不知道如何在json数据中表示枚举),因为哪种json模式表示它是一个数组.我希望它是枚举类型.

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.

那我如何在JSON数据中表示Enum类型?

推荐答案

JSON没有enum类型. enum的两种建模方式是:

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,
};

这篇关于JSON数据枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆