• 使用JsonProperty Attribute修改返回json


    使用JsonProperty Attribute修改返回 json 值的name

    本例使用JsonPropertyAttribute在序列化为JSON时更改属性的名称。

    public class Videogame
    {
        [JsonProperty("name")]
        public string Name { get; set; }
    
        [JsonProperty("release_date")]
        public DateTime ReleaseDate { get; set; }
    }
    
    Videogame starcraft = new Videogame
    {
        Name = "Starcraft",
        ReleaseDate = new DateTime(1998, 1, 1)
    };
    
    string json = JsonConvert.SerializeObject(starcraft, Formatting.Indented);
    
    Console.WriteLine(json);
    // {
    //   "name": "Starcraft",
    //   "release_date": "1998-01-01T00:00:00"
    // }
    

    排序

    public class Account
    {
        public string EmailAddress { get; set; }
    
        // appear last
        [JsonProperty(Order = 1)]
        public bool Deleted { get; set; }
    
        [JsonProperty(Order = 2)]
        public DateTime DeletedDate { get; set; }
    
        public DateTime CreatedDate { get; set; }
        public DateTime UpdatedDate { get; set; }
    
        // appear first
        [JsonProperty(Order = -2)]
        public string FullName { get; set; }
    }
    Account account = new Account
    {
        FullName = "Aaron Account",
        EmailAddress = "aaron@example.com",
        Deleted = true,
        DeletedDate = new DateTime(2013, 1, 25),
        UpdatedDate = new DateTime(2013, 1, 25),
        CreatedDate = new DateTime(2010, 10, 1)
    };
    
    string json = JsonConvert.SerializeObject(account, Formatting.Indented);
    
    Console.WriteLine(json);
    // {
    //   "FullName": "Aaron Account",
    //   "EmailAddress": "aaron@example.com",
    //   "CreatedDate": "2010-10-01T00:00:00",
    //   "UpdatedDate": "2013-01-25T00:00:00",
    //   "Deleted": true,
    //   "DeletedDate": "2013-01-25T00:00:00"
    // }
    

    在反序列化期间使用的Required,以验证是否存在所需的JSON属性

    public class Videogame
    {
        [JsonProperty(Required = Required.Always)]
        public string Name { get; set; }
    
        [JsonProperty(Required = Required.AllowNull)]
        public DateTime? ReleaseDate { get; set; }
    }
    string json = @"{
      'Name': 'Starcraft III',
      'ReleaseDate': null
    }";
    
    Videogame starcraft = JsonConvert.DeserializeObject<Videogame>(json);
    
    Console.WriteLine(starcraft.Name);
    // Starcraft III
    
    Console.WriteLine(starcraft.ReleaseDate);
    // null
    

    JsonIgnoreAttribute

    使用JsonIgnoreAttribute从序列化中排除属性

    public class Account
    {
        public string FullName { get; set; }
        public string EmailAddress { get; set; }
    
        [JsonIgnore]
        public string PasswordHash { get; set; }
    }
    

    详情请参考 https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm

  • 相关阅读:
    LinqPad 1.31 提供下载
    LINQ to SQL更新数据库操作
    电脑开机进入桌面很慢的解决办法
    VS2008的Linq更新数据就那么费劲
    将图片进行base64编码,并将接受编码后的图片转换为实际图片
    C# 2.0 之 static class
    测试杀毒软件的性能代码
    VS2008 Ajax Toolkit 控件(修正后转载)
    C#发送邮件程序 及测试邮件服务器
    asp.net中的<%%>形式的详细用法实例讲解
  • 原文地址:https://www.cnblogs.com/WNpursue/p/10755011.html
Copyright © 2020-2023  润新知