简体   繁体   English

如何在反序列化时字段为空时将默认值设置为Guid

[英]how to set default value as Guid when field is blank at the time of deserializing

I am trying to pull records from server via webservice in and in my model I have a parameter named public String mId {get;set;} and my JSON structure is as follows 我试图通过webservice从服务器中取出记录,在我的模型中,我有一个名为public String mId {get;set;} ,我的JSON结构如下

{  "name":"Jhon",  "mId":"",  "address":"55 jump street" }

Now when mId is blank i want to generate a new GUID for this field while deserializing json using Newton JSON as [DefaultValue("")] is accepting only constant value i can not use Guid.NewGuid() over there 现在,当mId为空时,我想为这个字段生成一个新的GUID ,同时使用Newton JSON反序列化json,因为[DefaultValue("")]只接受常量值,我不能在那里使用Guid.NewGuid()

That is not what DefaultValue is used for. 这不是DefaultValue的用途。 To add a default value: 要添加默认值:

string mId { get; set; } = Guid.NewGuid()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反序列化时如何将“序列化默认值”设置为“是”? - How can i set "Serialize Default Values" to Yes in when deserializing? XmlSerializer - 反序列化枚举时如何设置默认值? - XmlSerializer - How can I set a default when deserializing an enum? 使用自定义 ContractResolver,在将 null JSON 属性反序列化为值类型成员时如何设置默认值而不是 null? - Using a custom ContractResolver, how to set a default value instead of null when deserializing a null JSON property to a value-type member? System.Text.Json:反序列化时,如何忽略在构造函数中设置了默认值的属性的 null 或空字符串值? - System.Text.Json: when deserializing, how can I ignore a null or empty string value for a property that has a default value set in the constructor? 字典 <int, guid> 如何取值的Guid? - dictionary<int, guid> how to take value field that is Guid? 在C#(.NET 3.5)中反序列化xml时是否可以设置默认值? - Is it possible to set a default value when deserializing xml in C# (.NET 3.5)? 如何为隐藏字段设置默认值? - How to set a default value for a hidden field? Guid 的默认值是多少? - What is the default value for Guid? 为反序列化XML时为null的属性设置默认值 - Set default value for attributes which are null while deserializing XML 当带有垃圾字符的requset API时,引导字段设置为空 - Guid field set to empty when requset API with junk characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM