【发布时间】:2016-09-10 20:04:32
【问题描述】:
我有一个使用 WCF Rest 客户端生成的简单 JSON,但是当我尝试反序列化响应时,我在 JSON.Net 中收到错误 NullReferenceException。我有以下 JSON:
{"Code":-2146232800,"ExceptionType":"IOException","Message":"Msg","Stacktrace":"Some"}
以下课程:
[DataContract]
public class FileUploadError
{
public FileUploadError(Exception exception)
{
Code = exception.HResult;
ExceptionType = exception.GetType().Name;
Message = GetMessage(exception);
Stacktrace = exception.StackTrace;
if (exception.Data.Count > 0)
{
Data = string.Join(Environment.NewLine, exception.Data.Cast<DictionaryEntry>().Select(x => x.Key + "=" + x.Value));
}
}
private string GetMessage(Exception exception)
{
if (exception.InnerException == null)
{
return exception.Message;
}
const string delimiter = "->";
var sb = new StringBuilder(1024);
for (var ex = exception; ex != null; ex = ex.InnerException)
{
sb.Append(ex.Message).Append(delimiter);
}
sb.Length -= delimiter.Length;
return sb.ToString();
}
[DataMember(IsRequired = true)]
[JsonProperty("Code", NullValueHandling = NullValueHandling.Ignore)]
public int Code { get; set; }
[DataMember(IsRequired = true)]
[JsonProperty("ExceptionType", NullValueHandling = NullValueHandling.Ignore)]
public string ExceptionType { get; set; }
[DataMember(EmitDefaultValue = false)]
[JsonProperty("Message", NullValueHandling = NullValueHandling.Ignore)]
public string Message { get; set; }
[DataMember(EmitDefaultValue = false)]
[JsonProperty("Stacktrace", NullValueHandling = NullValueHandling.Ignore)]
public string Stacktrace { get; set; }
[DataMember(EmitDefaultValue = false)]
[JsonProperty("Data", NullValueHandling = NullValueHandling.Ignore)]
public string Data { get; set; }
}
然后我反序列化它:
const string text = "{\"Code\":-2146232800,\"ExceptionType\":\"IOException\",\"Message\":\"Msg\",\"Stacktrace\":\"Some\"}";
var obj = JsonConvert.DeserializeObject<FileUploadError>(text);
并得到主题错误。在新的控制台应用程序中复制,如果需要,我可以提供。 JSON 很简单,为什么会出现这个错误?
示例:https://dotnetfiddle.net/76FJd0
堆栈跟踪:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at FileUploadError..ctor(Exception exception) in d:\Windows\Temp\nclgvim3.0.cs:line 28
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectFromNonDefaultConstructor(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ConstructorInfo constructorInfo, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Program.Main(String[] args)
【问题讨论】:
-
@Nasreddine 你在打分之前读过吗?我提供了一个常量字符串,你认为常量非空字符串可以为空吗?
-
这是链接问题和答案的副本。如果您调试过代码,您会意识到
exception.HResult会抛出该异常,因为您的exception参数是null。由于您不问“为什么我的异常参数为空”,所以问题是重复的。 -
投票重新开放以删除“欺骗”标志。这可能是关于 Json.Net 反序列化的一些问题的重复,但它肯定不是以任何有用的方式与链接问题的重复。
-
@McKenzieG1 解决的是 OP 特定情况,而不是没有提供任何信息的
NullReferenceException。答案应该首先提供解决这个问题的方法。
标签: c# json wcf serialization