码迷,mamicode.com
首页 > 其他好文 > 详细

newtonsoft 序列化与反序列化

时间:2020-04-14 22:25:54      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:string   反序   set   convert   line   pre   public   object   mode   

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 序列化与反序列化

标签:string   反序   set   convert   line   pre   public   object   mode   

原文地址:https://www.cnblogs.com/wugh8726254/p/12701536.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!