Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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.NET-将EmptyOrWhiteSpace字符串属性转换为null_C#_.net_Json - Fatal编程技术网

C# Json.NET-将EmptyOrWhiteSpace字符串属性转换为null

C# Json.NET-将EmptyOrWhiteSpace字符串属性转换为null,c#,.net,json,C#,.net,Json,是否可以使用[JsonProperty]属性将任何空字符串或填充空格的字符串转换为null 比如: public class Request { [JsonProperty(NullOrWhiteSpaceValueHandling)] public string Description {get;set;} } 与渲染时跳过空值的方式相同。 当此属性为“空”时,不会设置值。您需要实现一个自定义的JsonConverter,并将其分配给JsonProperty属性的

是否可以使用[JsonProperty]属性将任何空字符串或填充空格的字符串转换为null

比如:

 public class Request
 {
     [JsonProperty(NullOrWhiteSpaceValueHandling)] 
     public string Description {get;set;}
 }
与渲染时跳过空值的方式相同。
当此属性为“空”时,不会设置值。

您需要实现一个自定义的
JsonConverter
,并将其分配给
JsonProperty
属性的
TrimmingConverter
属性。有一个编写客户
TrimmingConverter
detailed的示例。一旦实现了类似的功能,您应该能够设置
NullValueHandling
ItemConverterType
属性。这将确保转换器将修剪字符串,如果字符串为null、空或空白,则序列化时将忽略该字符串

public class Request
{
    [
       JsonProperty(NullValueHandling = NullValueHandling.Ignore, 
                    ItemConverterType = typeof(TrimmingConverter))
    ] 
    public string Description { get; set; }
}

这是的正式文档。

默认的
字符串是
null
NullValueHandling=NullValueHandling.Ignore
@DavidPine空字符串或空白字符串与
null
字符串…@RB不同。我知道,我正在尝试分享如何为
null
执行此操作。仍在寻找空白处理。