使用JSON.NET序列化/反序列化对象字典

我试图序列化/反序列化一个Dictionary<string, object>这似乎正常工作,如果对象是一个简单的types,但对象是更复杂的时候不工作。

我有这个class级:

 public class UrlStatus { public int Status { get; set; } public string Url { get; set; } } 

在我的字典中,我添加了一个List<UrlStatus>其中包含一个“redirect链”键和一些简单的string,其中键为“Status”,“Url”,“Parent Url”。 我从JSON.Net返回的string如下所示:

 {"$type":"System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib","Status":"OK","Url":"http://www.ehow.com/m/how_5615409_create-pdfs-using-bean.html","Parent Url":"http://www.ehow.com/mobilearticle35.xml","Redirect Chain":[{"$type":"Demand.TestFramework.Core.Entities.UrlStatus, Demand.TestFramework.Core","Status":301,"Url":"http://www.ehow.com/how_5615409_create-pdfs-using-bean.html"}]} 

我用来序列化的代码如下所示:

 JsonConvert.SerializeObject(collection, Formatting.None, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects, TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple }); 

反序列化我在做:

 JsonConvert.DeserializeObject<T>(collection, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects, TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple, }); 

字典回来很好,所有的string回来罚款,但列表没有得到正确的反序列化。 它只是回来了

 {[ { "$type": "XYZ.TestFramework.Core.Entities.UrlStatus, XYZ.TestFramework.Core", "Status": 301, "Url": "/how_5615409_create-pdfs-using-bean.html" } ]} 

当然,我可以重新设置这个string,我得到正确的对象,但是好像JSON.Net应该为我做了这个。 显然我做错了什么,但我不知道它是什么。

我认为这是Json.NET的旧版本中的一个错误。 如果您尚未使用最新版本,请升级并重试。

  public class UrlStatus { public int Status { get; set; } public string Url { get; set; } } [Test] public void GenericDictionaryObject() { Dictionary<string, object> collection = new Dictionary<string, object>() { {"First", new UrlStatus{ Status = 404, Url = @"http://www.bing.com"}}, {"Second", new UrlStatus{Status = 400, Url = @"http://www.google.com"}}, {"List", new List<UrlStatus> { new UrlStatus {Status = 300, Url = @"http://www.yahoo.com"}, new UrlStatus {Status = 200, Url = @"http://www.askjeeves.com"} } } }; string json = JsonConvert.SerializeObject(collection, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple }); Assert.AreEqual(@"{ ""$type"": ""System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib"", ""First"": { ""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"", ""Status"": 404, ""Url"": ""http://www.bing.com"" }, ""Second"": { ""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"", ""Status"": 400, ""Url"": ""http://www.google.com"" }, ""List"": { ""$type"": ""System.Collections.Generic.List`1[[Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests]], mscorlib"", ""$values"": [ { ""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"", ""Status"": 300, ""Url"": ""http://www.yahoo.com"" }, { ""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"", ""Status"": 200, ""Url"": ""http://www.askjeeves.com"" } ] } }", json); object c = JsonConvert.DeserializeObject(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple }); Assert.IsInstanceOfType(typeof(Dictionary<string, object>), c); Dictionary<string, object> newCollection = (Dictionary<string, object>)c; Assert.AreEqual(3, newCollection.Count); Assert.AreEqual(@"http://www.bing.com", ((UrlStatus)newCollection["First"]).Url); List<UrlStatus> statues = (List<UrlStatus>) newCollection["List"]; Assert.AreEqual(2, statues.Count); } } 

编辑,我只是​​注意到你提到了一个列表。 TypeNameHandling应该被设置为All。

文档: TypeNameHandling设置