.net json序列化 和反序列化
Newtonsoft.Json
JsonConvert.SerializeObject(entity);//序列化
Entity en = JsonConvert.DeserializeObject<Entity>(str);//反序列化
如果要更改 类中属性序列化之后的名称
[JsonProperty("firstname")] //or
[JsonProperty(PropertyName ="firstname")]
public string Name { get; set; }
如果要序列化过程中忽略null值的属性,则
//写在类中属性的上面(两种写法)
[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
[JsonProperty("property_name", NullValueHandling=NullValueHandling.Ignore)]
//for all properties in a class
//写在类头上面 注意:嵌套类的话子类不会生效
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]