private void Foo5() { Person p = new Person { Age = 10, Name = null, Sex = "男", IsMarry = false, Birthday = new DateTime(1991, 1, 2) }; JsonSerializerSettings jsetting = new JsonSerializerSettings(); string[] propNames = null; if (p.Age > 10) { propNames = new string[] { "Age", "IsMarry" }; } else { propNames = new string[] { "Age", "Sex" }; } jsetting.ContractResolver = new LimitPropsContractResolver(propNames); Console.WriteLine(JsonConvert.SerializeObject(p, Formatting.Indented, jsetting)); //jsetting.ContractResolver = new LimitPropsContractResolver(new string[] { "Age", "IsMarry" }); //Console.WriteLine(JsonConvert.SerializeObject(p, Formatting.Indented, jsetting)); }
private void Foo4() { Person p = new Person { Age = 10, Name = null, Sex = "男", IsMarry = false, Birthday = new DateTime(1991, 1, 2) }; JsonSerializerSettings jsetting = new JsonSerializerSettings(); jsetting.NullValueHandling = NullValueHandling.Ignore; Console.WriteLine(JsonConvert.SerializeObject(p, Formatting.Indented, jsetting)); }