如何告诉JSON.NET StringEnumConverter使用DisplayName? [英] How to tell JSON.NET StringEnumConverter to take DisplayName?
本文介绍了如何告诉JSON.NET StringEnumConverter使用DisplayName?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下模型:
public enum Status
{
[Display(Name = "Awaiting Approval")]
AwaitingApproval,
Rejected,
Accepted,
}
我在这样的模型中使用此枚举:
I use this enum in a model like this:
public class Docs
{
[Key]
public int Id { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public Status Status { get; set; }
}
现在可以正常工作了;序列化器返回等效于枚举的字符串.我的问题是如何告诉JSON.NET使用Display
属性而不是string
?
Now this works fine; the serializer returns the string equivalent of the enum. My question is how to tell JSON.NET to take the Display
attribute instead of the string
?
推荐答案
您应尝试使用[EnumMember]
而不是[Display]
.您还可以将[JsonConverter]
属性放在枚举本身上.
You should try using [EnumMember]
instead of [Display]
. You can also put the [JsonConverter]
attribute on the enum itself.
[JsonConverter(typeof(StringEnumConverter))]
public enum Status
{
[EnumMember(Value = "Awaiting Approval")]
AwaitingApproval,
Rejected,
Accepted,
}
这篇关于如何告诉JSON.NET StringEnumConverter使用DisplayName?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文