可能是重复问题:
使用Json.net - 部分自定义C#对象序列化
我有一个类,在使用asp.net MVC 4 WebAPI时,我成功地将其序列化为json.net。该类具有一个属性,它是一个字符串列表。
public class FruitBasket {
[DataMember]
public List<string> FruitList { get; set; }
public int FruitCount {
get {
return FruitList.Count();
}
}
}
在我的Get方法中,序列化正常进行,我在JSON中的FruitList属性中得到一个空数组,即[]。如果我在PUT请求的主体中使用相同的JSON,由于FruitList为空,在反序列化期间会在FruitCount属性中出现错误。
我希望FruitList属性(基本上是我的只读属性)进行序列化但不进行反序列化。有没有可能通过设置或其他方式来实现Json.net?