Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 发送/接收可为空的对象JSON webservice_C#_Json_Web Services_Nullable - Fatal编程技术网

C# 发送/接收可为空的对象JSON webservice

C# 发送/接收可为空的对象JSON webservice,c#,json,web-services,nullable,C#,Json,Web Services,Nullable,是否可以通过JSON WCF服务发送/接收可为空的对象,发送时的外观如何?那么发送/接收枚举又如何呢 例如,我们在下面有一个通过JSON webservice发送到用户ui的对象 public enum GenderTypes { Male = 0, Female = 1 } public class Human { public int? Age { get; set; } public GenderTypes Gender {

是否可以通过JSON WCF服务发送/接收可为空的对象,发送时的外观如何?那么发送/接收枚举又如何呢

例如,我们在下面有一个通过JSON webservice发送到用户ui的对象

  public enum GenderTypes
  {
      Male = 0,
      Female = 1
  }
  public class Human { 
      public int? Age { get; set; }
      public GenderTypes Gender {get; set;}
  }

是的,可以通过JSON WCF服务发送/接收可为空的对象

如果您的对象具有nullable type属性,并且您希望调用服务的客户机将其值作为null发送,那么您可以不在请求正文中传递该属性的值,然后反序列化您的对象

现在,您的对象将该属性的值设置为null

例如:您的请求正文如下所示(内容类型json)

当您将此请求反序列化到对象中时,对象将
obj.Age=12
obj.Gender=“M”
,现在如果您的请求与此类似

{ 
  "Gender":"M"
}
然后您的对象将具有
obj.Age=null
obj.Gender=“M”


(注意:这里我假设性别是字符串数据类型)

好的,谢谢,那么枚举呢?(如更新的示例中所示)对于枚举,可以像(本例中为0,1)一样传递枚举值
{ 
  "Gender":"M"
}