如何在 C# 中将 Enum 转换为带有 Header 的 Json 对象
How to convert Enum to Json object with Header in C#
这是我到目前为止所做的
public enum TCountryNames
{
[Display(Name="America")]
cnUSA = 1,
[Display(Name="England")]
cnUK,
[Display(Name="CHINA")]
cnCHN
}
public class MyClass
{
public static List<KeyValuePair<string, int>> GetEnumList()
{
var list = new List<KeyValuePair<string, int>>();
foreach (var e in Enum.GetValues(typeof(TCountryNames)))
{
list.Add(new KeyValuePair<string, int>(e.ToString(), (int)e));
}
return list;
}
}
Result: [cnUSA,1] with total count 3 and without header
我要的结果是[{"Id":1,"Name":"America"},{"Id":2,"Name":"England"}]
I've tried [JsonConverter(typeof(StringEnumConverter))]
public TCountryNames Names{ get; set; }
I've also tried converting enum to array list var names = Enum.GetValues(typeof(TCountryNames));
ArrayList arrLst = new ArrayList() { names };
but both of them doesn't seems to be working.
*任何帮助将不胜感激。先感谢您。 *
如果不想添加新的class
public static List<Dictionary<string, object>> GetEnumList()
{
var list = new List<Dictionary<string, object>>();
foreach (var e in Enum.GetValues(typeof(TCountryNames)))
{
list.Add(new Dictionary<string, object> { { "Id", (int)e }, { "Name", e.ToString() } });
}
return list;
}
定义序列化模型
public class EnumData
{
public int Id { get; set; }
public string Name { get; set; }
}
public static List<EnumData> GetEnumList()
{
var list = new List<EnumData>();
foreach (var e in Enum.GetValues(typeof(TCountryNames)))
{
list.Add(new EnumData { Id = (int)e, Name = e.ToString() });
}
return list;
}
要获取显示名称值,您应该使用 System.Reflection。然后你可以用简单的方式做到这一点:
public enum TCountryNames
{
[Display(Name = "America")]
cnUSA = 1,
[Display(Name = "England")]
cnUK,
[Display(Name = "CHINA")]
cnCHN
}
public class EnumData
{
public int Id { get; set; }
public string? Name { get; set; }
}
public class MyClass
{
public static List<EnumData> GetEnumList()
{
var list = new List<EnumData>();
foreach (var e in Enum.GetValues(typeof(TCountryNames)))
{
list.Add(new EnumData
{
Id = (int)e,
Name = e.GetType()
.GetMember(e.ToString())
.First()?
.GetCustomAttribute<DisplayAttribute>()?
.GetName()
});
}
return list;
}
}
所以澄清一下:
- 你为每个枚举创建循环
- 通过铸造获取id
- 使用反射命名 - 我添加了所有需要的防止空异常的保护
输出:
[
{
“编号”:1,
“名称”:“美国”
},
{
“编号”:2,
“名称”:“英国”
},
{
“编号”:3,
“名称”:“中国”
}
]
这是我到目前为止所做的
public enum TCountryNames
{
[Display(Name="America")]
cnUSA = 1,
[Display(Name="England")]
cnUK,
[Display(Name="CHINA")]
cnCHN
}
public class MyClass
{
public static List<KeyValuePair<string, int>> GetEnumList()
{
var list = new List<KeyValuePair<string, int>>();
foreach (var e in Enum.GetValues(typeof(TCountryNames)))
{
list.Add(new KeyValuePair<string, int>(e.ToString(), (int)e));
}
return list;
}
}
Result: [cnUSA,1] with total count 3 and without header
我要的结果是[{"Id":1,"Name":"America"},{"Id":2,"Name":"England"}]
I've tried [JsonConverter(typeof(StringEnumConverter))] public TCountryNames Names{ get; set; }
I've also tried converting enum to array list var names = Enum.GetValues(typeof(TCountryNames)); ArrayList arrLst = new ArrayList() { names }; but both of them doesn't seems to be working.
*任何帮助将不胜感激。先感谢您。 *
如果不想添加新的class
public static List<Dictionary<string, object>> GetEnumList() { var list = new List<Dictionary<string, object>>(); foreach (var e in Enum.GetValues(typeof(TCountryNames))) { list.Add(new Dictionary<string, object> { { "Id", (int)e }, { "Name", e.ToString() } }); } return list; }
定义序列化模型
public class EnumData { public int Id { get; set; } public string Name { get; set; } } public static List<EnumData> GetEnumList() { var list = new List<EnumData>(); foreach (var e in Enum.GetValues(typeof(TCountryNames))) { list.Add(new EnumData { Id = (int)e, Name = e.ToString() }); } return list; }
要获取显示名称值,您应该使用 System.Reflection。然后你可以用简单的方式做到这一点:
public enum TCountryNames
{
[Display(Name = "America")]
cnUSA = 1,
[Display(Name = "England")]
cnUK,
[Display(Name = "CHINA")]
cnCHN
}
public class EnumData
{
public int Id { get; set; }
public string? Name { get; set; }
}
public class MyClass
{
public static List<EnumData> GetEnumList()
{
var list = new List<EnumData>();
foreach (var e in Enum.GetValues(typeof(TCountryNames)))
{
list.Add(new EnumData
{
Id = (int)e,
Name = e.GetType()
.GetMember(e.ToString())
.First()?
.GetCustomAttribute<DisplayAttribute>()?
.GetName()
});
}
return list;
}
}
所以澄清一下:
- 你为每个枚举创建循环
- 通过铸造获取id
- 使用反射命名 - 我添加了所有需要的防止空异常的保护
输出: [ { “编号”:1, “名称”:“美国” }, { “编号”:2, “名称”:“英国” }, { “编号”:3, “名称”:“中国” } ]