Json 使用JsonProperty Attribute

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GongHuiNewtonsoft.Json;

namespace JSONDemo
{
public class Movie
{
[JsonProperty("name")]
public string Name { get; set; }

    [JsonProperty("Chinese Director")]  
    public string Director { get; set; }  
      
    public int ReleaseYear { get; set; }  
}  

}

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GongHuiNewtonsoft.Json;

namespace JSONDemo
{
public class Movie
{
[JsonProperty(Order=4)]
public string Name { get; set; }

    [JsonProperty(Order=0)]  
    public string Director { get; set; }  
      
    public int ReleaseYear { get; set; }  

    [JsonProperty(Order=-3)]  
    public string ChiefActor { get; set; }  

    [JsonProperty(Order=2)]  
    public string ChiefActress { get; set; }  
} 

三、JSON使用JsonPropertyAttribute反序列化属性时,Required指定属性性质

.创建一个Movie对象,给属性添加JsonProperty,并指定其Required的性质。属性Name必须有值,DateTime可以为空
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GongHuiNewtonsoft.Json;

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序列化引用类型集合
1.创建一个Director对象,并声明一个本身类型的属性,指定JsonProperty中的IsReference为true.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GongHuiNewtonsoft.Json;

namespace JSONDemo
{
public class Director
{
public string Name { get; set; }

    [JsonProperty(IsReference=true)]  
    public Director ExecuteDir { get; set; }  
}  

}
2.创建一个Movie对象,声明一个Director集合的属性,指定JsonProperty中的IsReference为true.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GongHuiNewtonsoft.Json;

namespace JSONDemo
{
public class Movie
{
public string Name { get; set; }

    [JsonProperty(ItemIsReference=true)]  
    public IList<Director> Directors { get; set; }  
}  

}

3.序列化对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GongHuiNewtonsoft.Json;
using GongHuiNewtonsoft.Json.Serialization;
using GongHuiNewtonsoft.Json.Converters;

namespace JSONDemo
{
class Program
{
static void Main(string[] args)
{
Director dir = new Director
{
Name = "冯小刚"
};

        Director dir1 = new Director  
        {  
            Name = "张艺谋",  
            ExecuteDir = dir  
        };  

        Movie m = new Movie  
        {  
            Name = "满城尽带黄金甲",  
            Directors = new List<Director>  
            {  
                dir,  
                dir1  
            }  
        };  

        string json = JsonConvert.SerializeObject(m, Formatting.Indented);  
        Console.WriteLine(json);  
    }  
}  

}

五、JSON使用JsonPropertyAttribute序列化忽略属性null

1.创建一个Movie对象,并在属性上指定JsonProperty,添加NullValueHandling,忽略null

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GongHuiNewtonsoft.Json;

namespace JSONDemo
{
public class Movie
{
public string Name { get; set; }

    public string Director { get; set; }  

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

}

2.实例化对象Movie对象,然后序列化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GongHuiNewtonsoft.Json;
using GongHuiNewtonsoft.Json.Serialization;
using GongHuiNewtonsoft.Json.Converters;

namespace JSONDemo
{
class Program
{
static void Main(string[] args)
{
Movie m = new Movie
{
Name = "爱情呼叫转移",
Director = "张建亚"
};

        string json = JsonConvert.SerializeObject(m, Formatting.Indented);  
        Console.WriteLine(json);  
    }  
}  

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,727评论 0 3
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,993评论 19 139
  • 系列文章: EF-ModelFirst实现过程+数据库迁移 http://www.jianshu.com/p/2a...
    垃圾简书_吃枣药丸阅读 3,936评论 4 24
  • 当我的身体化成 平静的波涌的 黑色的海 生长在我的右岸的 温柔的樱花 我要将它 飘在我的海上 我想将它 继续飘在我...
    ratshen阅读 90评论 0 0
  • 在世人的眼里,柳市镇是全国有名的“电器之都”,是温州五大镇级市之一,多年以来,其一直傲居温州综合实力第一强镇。 柳...
    544767782600阅读 1,279评论 0 1