c#使用Newtonsoft.Json.JsonConvert序列化复杂类型

时间:2014-03-30 18:42:53

标签: c# json serialization json.net

我是C#的新手,也是使用Newtonsoft.Json.JsonConvert,JsonConverter的新手。我需要序列化一个复杂类型,其定义如下:

public class UserDetails
{
        public string _name;
        public Dictionary<string, List<TaskDetails>> _userSchedule;
}

public class TaskDetails
{
        public string _taskDescription;
        public DateTime _deadLine;

}

当我尝试序列化UserDetails时,序列化字符串没有所有细节。我的单元测试如下:

UserDetails obj = new UserDetails("abc");
Dictionary<string, List<TaskDetails>> _schedules = new Dictionary<string,List<TaskDetails>>();

List<TaskDetails> _sched1 = new List<TaskDetails>();
_sched1.Add(new TaskDetails("pickup", new DateTime(2014, 04, 10)));
_sched1.Add(new TaskDetails("store", new DateTime(2014, 04, 15)));

List<TaskDetails> _sched2 = new List<TaskDetails>();
_sched2.Add(new TaskDetails("write doc", new DateTime(2014, 04, 15)));

_schedules.Add("external activity", _sched1);
_schedules.Add("internal", _sched2);

序列化期间,

var settings = new JsonSerializerSettings();
settings.TypeNameHandling = TypeNameHandling.Objects;

I see that doing the one below is not enough. It serializes the dictionary but the custom Type within the 'List' is ignored. And I need to use JsonConverter, but not sure how to do that. 

JsonConvert.SerializeObject(_schedules, Formatting.Indented, settings);

你能提供一些指示吗?

此致 杰

0 个答案:

没有答案