我有一个DTO类,我序列化
Json.Serialize(MyClass)
我如何排除该物业的公共财产?
(它必须是公共的,因为我在其他地方的代码中使用它)
我有一个DTO类,我序列化
Json.Serialize(MyClass)
我如何排除该物业的公共财产?
(它必须是公共的,因为我在其他地方的代码中使用它)
当前回答
如果你在。net框架中使用System.Web.Script.Serialization,你可以把ScriptIgnore属性放在不应该被序列化的成员上。请看下面的例子:
考虑以下(简化)情况: 公共类用户{ 公共int Id{获取;设置;} 公共字符串名称{获取;设置;} (ScriptIgnore) public bool IsComplete { string.IsNullOrEmpty(Name);} } } 在这种情况下,只有Id和Name属性会被序列化,因此生成的JSON对象看起来像这样: {Id: 3,名称:'测试用户'}
另外,不要忘记添加对“System.Web”的引用。扩展”
其他回答
如果你在。net框架中使用System.Web.Script.Serialization,你可以把ScriptIgnore属性放在不应该被序列化的成员上。请看下面的例子:
考虑以下(简化)情况: 公共类用户{ 公共int Id{获取;设置;} 公共字符串名称{获取;设置;} (ScriptIgnore) public bool IsComplete { string.IsNullOrEmpty(Name);} } } 在这种情况下,只有Id和Name属性会被序列化,因此生成的JSON对象看起来像这样: {Id: 3,名称:'测试用户'}
另外,不要忘记添加对“System.Web”的引用。扩展”
为dotnet核心添加System.Text.Json版本
对于编译时,根据上面的答案添加[JsonIgnore]。
对于运行时,需要将JsonConverter添加到选项中。
首先,为要排除的类型创建一个JsonConverter,例如下面的ICollection<LabMethod>
public class LabMethodConverter : JsonConverter<ICollection<LabMethod>>
{
public override ICollection<LabMethod> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
//deserialize JSON into a ICollection<LabMethod>
return null;
}
public override void Write(Utf8JsonWriter writer, ICollection<LabMethod> value, JsonSerializerOptions options)
{
//serialize a ICollection<LabMethod> object
writer.WriteNullValue();
}
}
然后在序列化Json时添加到选项
var options = new JsonSerializerOptions();
options.Converters.Add(new LabMethodConverter());
var new_obj = Json(new { rows = slice, total = count }, options);
你可以使用[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和名称只会被序列化
抱歉,我决定再写一个答案,因为其他答案都不能复制粘贴。
如果你不想用一些属性装饰属性,或者如果你无法访问类,或者如果你想决定在运行时序列化什么,等等,等等,这里是你如何在Newtonsoft做它。Json
//short helper class to ignore some properties from serialization
public class IgnorePropertiesResolver : DefaultContractResolver
{
private readonly HashSet<string> ignoreProps;
public IgnorePropertiesResolver(IEnumerable<string> propNamesToIgnore)
{
this.ignoreProps = new HashSet<string>(propNamesToIgnore);
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
if (this.ignoreProps.Contains(property.PropertyName))
{
property.ShouldSerialize = _ => false;
}
return property;
}
}
使用
JsonConvert.SerializeObject(YourObject, new JsonSerializerSettings()
{ ContractResolver = new IgnorePropertiesResolver(new[] { "Prop1", "Prop2" }) });
注意:如果您决定使用这个答案,请确保缓存ContractResolver对象,否则性能可能会受到影响。
我在这里发布了代码,以防有人想要添加任何东西
https://github.com/jitbit/JsonIgnoreProps
如果你使用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/efficient-json-with-json-net-reducing-serialized-json-size