我有一个我序列化的DTO类
Json.Serialize(MyClass)
我怎么能排除它public的财产呢?
(It has to be public, as I use it in my code somewhere else)
我有一个我序列化的DTO类
Json.Serialize(MyClass)
我怎么能排除它public的财产呢?
(It has to be public, as I use it in my code somewhere else)
If you are using Json.Net attribute [JsonIgnore]
will simply ignore the field/property while serializing or deserialising.
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; }
}
Refer http://james.newtonking.com/archive/2009/10/23/efficient-json-with-json-net-reducing-serialized-json-size for more details