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);