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; }
}

}
'''


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM