newtonsoft 序列化与反序列化
Posted wugh8726254
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了newtonsoft 序列化与反序列化相关的知识,希望对你有一定的参考价值。
public class Staff
{
public string Name { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public string DepartmentName { get; set; }
public Staff Leader { get; set; }
}
Staff jack = new Staff { Name = "Jack", Age = 31, Gender = "Male", DepartmentName = "Personnel Department", Leader = null };
var jSetting = new JsonSerializerSettings();
jSetting.NullValueHandling = NullValueHandling.Ignore;
string json = JsonConvert.SerializeObject(jack,jSetting);
Console.WriteLine(json);
1序列化
A a=new A(); a.Name="Elain00"; a.Hobby="eat eat"; string jsonStr=JsonConvert.SerializeObject(a);
2反序列化
string jsonstr = "jsonString"; Class model = JsonConvert.DeserializeObject<Class>(jsonstr); string JsonStr=‘"{‘Name‘:‘Elaine00‘,‘Hobby‘:‘eat eat‘}"; A a=JsonConvert.DeserializeObject<A>(JsonStr);
以上是关于newtonsoft 序列化与反序列化的主要内容,如果未能解决你的问题,请参考以下文章
一:Newtonsoft.Json 支持序列化与反序列化的.net 对象类型;