https://www.newtonsoft.com/json
1.把对象转成json字符串
Student student = new Student(); student.age = 10; student.name = "tom"; student.list = new List<string> { "a", "b" }; // string json = JsonConvert.SerializeObject(student); Console.WriteLine(json);
{"list":["a","b"],"name":"tom","age":10}
2,把json字符串转成对象
Student a =JsonConvert.DeserializeObject<Student>(json);
3.时间问题
序列化时间会带T
框架中设置
services.AddControllers().AddNewtonsoftJson(options => { options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; });
单独设置
JsonSerializerSettings setting = new JsonSerializerSettings(); JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() => { setting.DateFormatString = "yyyy-MM-dd HH:mm:ss"; setting.DateParseHandling = DateParseHandling.DateTimeOffset; return setting; });