我有一个 DTO 类,我将其序列化
Json.Serialize(MyClass)
我怎样才能排除public它的财产?
(它必须是公开的,因为我在其他地方的代码中使用它)
如果您使用 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; }
}
Refer http://james.newtonking.com/archive/2009/10/23/efficient-json-with-json-net-reducing-serialized-json-size http://james.newtonking.com/archive/2009/10/23/efficient-json-with-json-net-reducing-serialized-json-size更多细节
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)