如何从Json序列化中排除属性

我有一个DTO课,我串行化

Json.Serialize(MyClass) 

我怎样才能排除它的公共财产?

(它必须是公开的,因为我在其他地方的代码中使用它)

您可以将ScriptIgnore属性放在不应被序列化的成员上。 看从这里采取的例子:

考虑以下(简化)情况:

 public class User { public int Id { get; set; } public string Name { get; set; } [ScriptIgnore] public bool IsComplete { get { return Id > 0 && !string.IsNullOrEmpty(Name); } } } 

在这种情况下,只有Id和Name属性会被序列化,因此生成的JSON对象如下所示:

 { Id: 3, Name: 'Test User' } 

PS。 不要忘了添加一个对“ System.Web.Extensions ”的引用来使其工作

如果你正在使用Json.Net,在序列化或反序列化的时候,属性[JsonIgnore]就会忽略字段/多媒体。

 public class Car { // included in JSON public string Model { get; set; } public DateTime Year { get; set; } public List<string> Features { get; set; } // ignored [JsonIgnore] public DateTime LastModified { get; set; } } 

或者,您可以使用DataContract和DataMember属性select序列化/反序列化属性/字段。

 [DataContract] public class Computer { // included in JSON [DataMember] public string Name { get; set; } [DataMember] public decimal SalePrice { get; set; } // ignored public string Manufacture { get; set; } public int StockCount { get; set; } public decimal WholeSalePrice { get; set; } public DateTime NextShipmentDate { get; set; } } 

请参阅http://james.newtonking.com/archive/2009/10/23/efficient-json-with-json-net-reducing-serialized-json-size了解更多详情;

你可以使用[ScriptIgnore]

 public class User { public int Id { get; set; } public string Name { get; set; } [ScriptIgnore] public bool IsComplete { get { return Id > 0 && !string.IsNullOrEmpty(Name); } } } 

在这里引用

在这种情况下,Id和名称将只被序列化

如果你不那么热衷于用Attribute来装饰代码,尤其是当你不能在编译时告诉将会发生什么是我的解决scheme。

使用Javascript序列化程序

  public static class JsonSerializerExtensions { public static string ToJsonString(this object target,bool ignoreNulls = true) { var javaScriptSerializer = new JavaScriptSerializer(); if(ignoreNulls) { javaScriptSerializer.RegisterConverters(new[] { new PropertyExclusionConverter(target.GetType(), true) }); } return javaScriptSerializer.Serialize(target); } public static string ToJsonString(this object target, Dictionary<Type, List<string>> ignore, bool ignoreNulls = true) { var javaScriptSerializer = new JavaScriptSerializer(); foreach (var key in ignore.Keys) { javaScriptSerializer.RegisterConverters(new[] { new PropertyExclusionConverter(key, ignore[key], ignoreNulls) }); } return javaScriptSerializer.Serialize(target); } } public class PropertyExclusionConverter : JavaScriptConverter { private readonly List<string> propertiesToIgnore; private readonly Type type; private readonly bool ignoreNulls; public PropertyExclusionConverter(Type type, List<string> propertiesToIgnore, bool ignoreNulls) { this.ignoreNulls = ignoreNulls; this.type = type; this.propertiesToIgnore = propertiesToIgnore ?? new List<string>(); } public PropertyExclusionConverter(Type type, bool ignoreNulls) : this(type, null, ignoreNulls){} public override IEnumerable<Type> SupportedTypes { get { return new ReadOnlyCollection<Type>(new List<Type>(new[] { this.type })); } } public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer) { var result = new Dictionary<string, object>(); if (obj == null) { return result; } var properties = obj.GetType().GetProperties(); foreach (var propertyInfo in properties) { if (!this.propertiesToIgnore.Contains(propertyInfo.Name)) { if(this.ignoreNulls && propertyInfo.GetValue(obj, null) == null) { continue; } result.Add(propertyInfo.Name, propertyInfo.GetValue(obj, null)); } } return result; } public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) { throw new NotImplementedException(); //Converter is currently only used for ignoring properties on serialization } }