在项目中用到了Newtonsoft.Json来实现序列化和反序列化,在这里写下实现代码. 1.创建类用于排除不序列化的属性 public class ExcludePropertiesContractResolver : DefaultContractResolver { IEnumerable<string> lstExclude; public ExcludePropertiesContractResolver(IEnumerable<string> excludedPrope…
我们可以通过DataContractJsonSerializer类来序列化一个对象为json字符串. public class JsonConvert<T> { public static string ObjectToJson(T obj) { var serializer = new DataContractJsonSerializer(typeof(T)); var stream = new MemoryStream(); serializer.WriteObject(stream, o…