问题描述
我有一个我的api响应的模型,它具有枚举类型的属性。枚举属性仅在某些情况下填充,其余的情况下它的默认值为0输出
{
prop1: ABCD
prop2:0 ////这是枚举属性默认为零
}
我尝试使用自定义jsonconverter但是这并没有真正解决问题,因为我只能为它分配一个字符串值。
如果具有默认值,我如何从响应中排除枚举属性。
p>这是解决了做两件事
1。使枚举属性为空,并将其在构造函数中初始化为null。结果2。使用json serializer设置NullValueHandling = NullValueHandling.Ignore
I have a model for my api response which has a property of type enum. The enum property gets populated in only some scenarios rest of the scenario it gets the default value of 0
output{
prop1: "ABCD"
prop2: 0 //// this is enum property defaulting to zero
}
I tried using the custom jsonconverter but this does not really solve the problem as I can only assign a string value to it.
How can i exclude the enum property from the response if it has the default value.
This is resolved doing two things
1. Make the enum property as nullable and initialized it in the constructor to null.
2. Use json serializer setting NullValueHandling = NullValueHandling.Ignore
这篇关于使用json响应中的默认值排除枚举属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!