我有一个要序列化的DTO类
Json.Serialize(MyClass)如何排除它的公共财产?
(它必须是公共的,因为我在其他地方的代码中使用了它)
IgnoreDataMember ScriptIgnore JsonIgnore取决于您使用的序列化程序
                我有一个要序列化的DTO类
Json.Serialize(MyClass)如何排除它的公共财产?
(它必须是公共的,因为我在其他地方的代码中使用了它)
IgnoreDataMember ScriptIgnore JsonIgnore取决于您使用的序列化程序
                Answers:
如果System.Web.Script.Serialization在.NET框架中使用,则可以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” 的引用以使其起作用
ScriptIgnore在System.Web.Script.Serialization命名空间中找到。
                    如果您使用的是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属性来选择性地序列化/反序列化属性/字段。
[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/ficient-json-with-json-net-reducing-serialized-json-size
IgnoreDataMember不适用于默认的JsonResult序列化程序。
                    您可以使用[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和名称将仅被序列化
[ScriptIgnore]什么应该对物业,如果你的控制器使用基本MVC控制器使用return Json(...?
                    [ScriptIgnore]在MVC Controller中使用。但是请注意,如果您使用SignalR,那么也应该使用[JsonIgnore]。
                    抱歉,我决定写另一个答案,因为其他答案都没有足够的可粘贴粘贴性。
如果您不想用某些属性来装饰属性,或者您无法访问该类,或者您想确定运行时要序列化的内容,等等。等等。这是在Newtonsoft.Json中的操作方法。
//short helper class to ignore some properties from serialization
public class IgnorePropertiesResolver : DefaultContractResolver
{
    private IEnumerable<string> _propsToIgnore;
    public IgnorePropertiesResolver(IEnumerable<string> propNamesToIgnore)
    {
        _propsToIgnore = propNamesToIgnore;
    }
    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        JsonProperty property = base.CreateProperty(member, memberSerialization);
        property.ShouldSerialize = (x) => { return !_propsToIgnore.Contains(property.PropertyName); };
        return property;
    }
}用法
JsonConvert.SerializeObject(YourObject, new JsonSerializerSettings()
    { ContractResolver = new IgnorePropertiesResolver(new[] { "Prop1", "Prop2" }) };);如果有人要添加任何内容,我已经在此处发布了代码
https://github.com/jitbit/JsonIgnoreProps
重要更新:ContractResolver如果决定使用此答案,请确保缓存对象,否则可能会降低性能。
如果您不太希望像我一样使用Attributes装饰代码,尤其是当您无法在编译时告诉我将要发生的情况时。
使用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
        }
    }PropertyExclusionConverter可以变成一个PropertyInclusionConverter。
                    如果您正在使用,System.Text.Json则可以使用[JsonIgnore]。
FQ:System.Text.Json.Serialization.JsonIgnoreAttribute  
官方Microsoft文档:JsonIgnoreAttribute
如前所述这里:
该库是.NET Core 3.0共享框架的一部分内置。
对于其他目标框架,请安装System.Text.Json NuGet包。该软件包支持:
- .NET Standard 2.0和更高版本
- .NET Framework 4.6.1和更高版本
- .NET Core 2.0、2.1和2.2
您也可以使用[NonSerialized]属性
[Serializable]
public struct MySerializableStruct
{
    [NonSerialized]
    public string hiddenField;
    public string normalField;
}指示不应序列化可序列化类的字段。这个类不能被继承。
例如,如果您使用的是Unity(这不仅适用于Unity),则可以使用UnityEngine.JsonUtility
using UnityEngine;
MySerializableStruct mss = new MySerializableStruct 
{ 
    hiddenField = "foo", 
    normalField = "bar" 
};
Debug.Log(JsonUtility.ToJson(mss)); // result: {"normalField":"bar"}