Newtonsoft.Json笔记-JsonPropertyAttribute

一、JSON使用JsonPropertyAttribute重命名属性名

[JsonProperty("name")]

二、JSON使用JsonPropertyAttribute序列化升序排序属性

[JsonProperty(Order=4)]

三、反序列化属性时,Required指定属性性质
指定其Required的性质。属性Name必须有值,DateTime可以为空.

namespace JSONDemo
{    
    public class Movie
    {
        [JsonProperty(Required=Required.Always)]
        public string Name { get; set; }
 
        [JsonProperty(Required = Required.AllowNull)]
        public DateTime? ReleaseDate { get; set; }
 
        public string Director { get; set; }
    }
}

四、JSON使用JsonPropertyAttribute序列化引用类型集合
声明一个本身类型的属性,指定JsonProperty中的IsReference为true?

namespace JSONDemo
{    
    public class Movie
    {
        public string Name { get; set; }
 
        [JsonProperty(ItemIsReference=true)]
        public IList<Director> Directors { get; set; }
    }
}

五、序列化忽略属性null
在属性上指定JsonProperty,添加NullValueHandling,忽略null
'''
public class Movie
{
public string Name { get; set; }

    public string Director { get; set; }

    [JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
    public DateTime? LaunchDate { get; set; }
}

}
'''

原文地址:https://www.cnblogs.com/fanfan-90/p/13866684.html