使用Json.net进行序列化主要是为了解决成员对象循环引用的问题
给出一个例子:
要注意的有两点:
一是对要序列化的成员加上Attribute : [JsonProperty],要序列化的类为了循环引用要加上[DataContract(IsReference=true)]
二是[JsonConverter(typeof(MyIntQuoteConverter))],这个是指定转换协议,对一些特殊类型的变量比如时间,我们可以通过这个类改变他在序列化后的格式
[DataContract(IsReference=true)] public class Programme { /// <summary> /// 构造函数 /// </summary> public Programme() { } /// <summary> /// /// </summary> [JsonProperty] [JsonConverter(typeof(MyIntQuoteConverter))] public virtual Int64 proid { get; set; } /// <summary> /// 节目名称 /// </summary> [JsonProperty] public virtual string proname { get { return this.program_module.cl_pro_modulename; } set { proname = value; } } /// <summary> /// 节目日期 /// </summary> [JsonProperty] public virtual string prodate { get; set; } /// <summary> /// 节目模板号 /// </summary> [JsonProperty] [JsonConverter(typeof(MyIntQuoteConverter))] public virtual int pro_moduleid { get; set; } /// <summary> /// 主持人列表 /// </summary> [JsonProperty] public virtual IList<Host> host { get { return this.program_module.hostinfos; } set { host = value; } } /// <summary> /// 节目图片地址 /// </summary> [JsonProperty] public virtual string pro_imgurl { get; set; } /// <summary> /// 节目图标地址 /// </summary> [JsonProperty] public virtual string pro_thumbimgurl { get; set; } /// <summary> /// 嘉宾信息 /// </summary> [JsonProperty] public virtual string guests { get; set; } /// <summary> /// 节目描述 /// </summary> [JsonProperty] public virtual ProgramTopic proTopic { get; set; } /// <summary> /// yyyy-MM-dd HH:mm:ss,部分节目跨零点,但属于前一天的节目单,实际时间是一个带日期的详细时间,用于查询某天的节目单 /// </summary> [JsonProperty] [JsonConverter(typeof(MyJsonDateTimeConverter))] public virtual DateTime probegin { get; set; } /// <summary> /// yyyy-MM-dd HH:mm:ss,部分节目跨零点,但属于前一天的节目单,实际时间是一个带日期的详细时间,用于查询某天的节目单 /// </summary> [JsonProperty] [JsonConverter(typeof(MyJsonDateTimeConverter))] public virtual DateTime proend { get; set; } /// <summary> /// 当前时间 /// </summary> [JsonProperty] [JsonConverter(typeof(MyJsonDateTimeConverter))] public virtual DateTime procurrent { get { return DateTime.Now; } } /// <summary> /// 节目模板 /// </summary> public virtual ProgrammeModule program_module { get; set; } /// <summary> /// 频率 /// </summary> public virtual Radio radio { get; set; } /// <summary> /// 返回一个表示节目实际时间的字符串 /// </summary> /// <returns></returns> public virtual string getProgrammePeriod() { string result = probegin.ToString("yyyy/MM/dd HH:mm") + "-" + proend.ToString("HH:mm"); return result; } }
public virtual string ToJson(var obj) { StringWriter sw = new StringWriter(); JsonSerializer serializer = JsonSerializer.Create( new JsonSerializerSettings { Converters = new JsonConverter[] { new MyJsonDateTimeConverter() }, ReferenceLoopHandling = ReferenceLoopHandling.Serialize, NullValueHandling = NullValueHandling.Include, PreserveReferencesHandling = PreserveReferencesHandling.Objects, ContractResolver = new DefaultContractResolver() } ); using (JsonWriter jsonWriter = new JsonTextWriter(sw)) { jsonWriter.Formatting = Formatting.Indented; serializer.Serialize(jsonWriter, obj); } return sw.ToString(); }