我一直试图解决这个问题几个小时。我尝试了很多方法并阅读了很多帖子。我似乎无法找到解决方案。
我有一个对象层次结构,我有派生类型包含另一个派生类型。我正在使用Json.Net的TypeNameHandling
设置。我对序列化和反序列化都设置为Auto
。
生成的JSON是:
"[\r\n {\r\n \"$type\": \"Common.Monitors.HeartbeatMonitor, Common\",\r\n \"ClassName\": \"HeartbeatMonitor\",\r\n \"ServerGroupMonitorId\": 1,\r\n \"Instructions\": {\r\n \"$type\": \"Common.Monitors.HeartbeatMonitorInstructions, Common\",\r\n \"RunIntervalInMinutes\": 1\r\n },\r\n \"LastExecutionDateTime\": \"2016-03-22T16:35:18.7458519\"\r\n },\r\n {\r\n \"$type\": \"Common.Monitors.DiskSpaceMonitor, Common\",\r\n \"ClassName\": \"DiskSpaceMonitor\",\r\n \"ServerGroupMonitorId\": 2,\r\n \"Instructions\": {\r\n \"$type\": \"Common.Monitors.DiskSpaceMonitorInstructions, Common\",\r\n \"DriveLetter\": \"C:\\\",\r\n \"AlertWhenPercentFreeLessThan\": 20,\r\n \"RunIntervalInMinutes\": 30\r\n },\r\n \"LastExecutionDateTime\": \"2016-03-22T16:15:18.7458519\"\r\n }\r\n]"
尝试使用以下内容反序列化时:
string json = response.Content.ReadAsStringAsync().Result;
IEnumerable<MonitorBase> monitors = JsonConvert.DeserializeObject<IEnumerable<MonitorBase>>(json, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});
我收到以下例外:
类型&#39; Newtonsoft.Json.JsonSerializationException&#39;的例外情况发生在Newtonsoft.Json.dll但未在用户代码中处理。
其他信息:转换值时出错
"[ { "$type": "Common.Monitors.HeartbeatMonitor, Common", "ClassName": "HeartbeatMonitor", "ServerGroupMonitorId": 1, "Instructions": { "$type": "Common.Monitors.HeartbeatMonitorInstructions, Common", "RunIntervalInMinutes": 1 }, "LastExecutionDateTime": "2016-03-22T16:35:18.7458519" }, { "$type": "Common.Monitors.DiskSpaceMonitor, Common", "ClassName": "DiskSpaceMonitor", "ServerGroupMonitorId": 2, "Instructions": { "$type": "Common.Monitors.DiskSpaceMonitorInstructions, Common", "DriveLetter": "C:\\", "AlertWhenPercentFreeLessThan": 20, "RunIntervalInMinutes": 30 }, "LastExecutionDateTime": "2016-03-22T16:15:18.7458519" } ]"
键入&#39; System.Collections.Generic.IEnumerable`1 [Common.Monitors.MonitorBase]&#39;。 路径&#39;&#39;,第1行,第943位。
HeartbeatMonitor
和DiskSpaceMonitor
类型派生自MonitorBase
,其各自的Instructions
类型派生自MonitorInstructionBase
。
我无能为力,但我认为我遗漏了一些明显的东西,因为我无法想象这不是一个相当常见的场景。
答案 0 :(得分:3)
我认为问题可能是您的JSON是双序列化的,如转义引号\"
,可见换行\r\n
以及引用整个JSON的事实所证明。反序列化双序列化JSON时,结果是字符串,而不是对象,显然无法将其转换为任何类型的IEnumerable<T>
。因此错误。
如果您控制JSON的来源,请确保您只对序列化对象一次。问题的一个常见原因是没有意识到某些框架(如Web API)会自动为您执行序列化,因此如果您在返回对象之前先手动序列化对象,那么您最终会得到双序列化的JSON。
如果您不控制JSON的来源,和/或您无法让所有者修复它,那么您可以通过反序列化JSON两次在客户端进行纠正:首先获取&#34;真实&#34; JSON字符串,然后是第二个从中生成对象的字符串。例如:
string escapedJson = response.Content.ReadAsStringAsync().Result;
string realJson = JsonConvert.DeserializeObject<string>(escapedJson);
IEnumerable<MonitorBase> monitors =
JsonConvert.DeserializeObject<IEnumerable<MonitorBase>>(realJson,
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});
修改强>
我从您的评论中看到,您是手动序列化对象以便使用Json.Net的TypeNameHandling设置,并且您正在寻找避免双序列化的方法。
您基本上有两个选择:
将TypeNameHandling
设置添加到Web API的全局序列化设置中,并从您的方法中删除手动序列化代码。为此,请在Application_Start()
中的global.asax
方法中添加以下内容。
var settings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
settings.TypeNameHandling = TypeNameHandling.Auto;
或者,您可以更改Web API方法,以创建并返回HttpResponseMessage
,其中StringContent
对象包含您的JSON,mediaType
设置为application/json
}。这可能会阻止Web API尝试重新序列化结果。
string json = JsonConvert.SerializeObject(monitors, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
Formatting = Formatting.Indented
});
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(json, Encoding.UTF8, "application/json");
return response;