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