2017-04-27 92 views

回答

4

是的。当您用[JsonObjectAttribute]标记您的班级并通过MemberSerialization.OptIn参数时,成员serialization is opt-in。然后用[JsonProperty]标记您的成员以将其包括在序列化中。

[JsonObject(MemberSerialization.OptIn)] 
public class Person 
{ 
    [JsonProperty] 
    public string Name { get; set; } 

    // not serialized because mode is opt-in 
    public string Department { get; set; } 
} 
+1

或者,人们可以使用[数据契约属性](HTTP://www.newtonsoft。 com/json/help/html/DataContractAndDataMember.htm)具有相同的效果。 – dbc

+0

@dbc我喜欢Json属性的替代品。我选择了Json属性,因为它们允许将来进一步轻松定制,并且他们也会采用相同的努力来应用。数据合同属性的优点是它们已经被大多数用户所熟悉。 – Aphelion

1

MemberSerialization.OptIn一个替代方案是使用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