Newtonsoft json.net反序列化NullReferenceException

亚历克斯·朱可夫斯基(Alex Zhukovskiy)

我有一个用WCF Rest客户端生成的简单JSON,但是当我尝试反序列化响应时NullReferenceException,JSON.Net中出现错误我有以下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);

并得到subj错误。转载在新的控制台应用程序中,如有需要,我可以提供。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)
数据库

您需要在类中添加一个公共的无参数构造函数FileUploadError

public class FileUploadError
{
    public FileUploadError()
    {
    }

或者,您可以将其设为私有并使用[JsonConstructor]

public class FileUploadError
{
    [JsonConstructor]
    private FileUploadError()
    {
    }

或者,将其保留为私有并反序列化ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor

var settings = new JsonSerializerSettings
{
    ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
};
var obj = JsonConvert.DeserializeObject<FileUploadError>(text, settings);

和:

public class FileUploadError
{
    private FileUploadError()
    {
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Newtonsoft json.net反序列化NullReferenceException

来自分类Dev

Newtonsoft JSON.NET反序列化错误

来自分类Dev

使用 NewtonSoft 反序列化 JSON

来自分类Dev

使用Newtonsoft.Json反序列化Json数组

来自分类Dev

反序列化Json Array Newtonsoft.Json

来自分类Dev

在C#中使用Newtonsoft Json.NET反序列化JSON

来自分类Dev

Newtonsoft JSON.net反序列化错误,其中JSON中的字段更改顺序

来自分类Dev

在C#中使用Newtonsoft Json.NET反序列化JSON

来自分类Dev

JSON反序列化(NewtonSoft JSON.NET)到XML失败

来自分类Dev

使用newtonsoft.json反序列化List <AbstractClass>

来自分类Dev

newtonsoft.json反序列化逻辑

来自分类Dev

Newtonsoft JSON使用HttpWebResponse反序列化

来自分类Dev

Newtonsoft JSON反序列化-密钥作为属性

来自分类Dev

newtonsoft json反序列化的问题(崩溃)

来自分类Dev

用newtonsoft json问题反序列化列表

来自分类Dev

用Newtonsoft.Json枚举反序列化

来自分类Dev

newtonsoft json模式反序列化ValidationError

来自分类Dev

Newtonsoft JSON反序列化以从列表中键入

来自分类Dev

Newtonsoft JSON使用HttpWebResponse反序列化

来自分类Dev

用newtonsoft反序列化json数组

来自分类Dev

用Newtonsoft.Json反序列化

来自分类Dev

Newtonsoft JSON Schema忽略用于反序列化的验证

来自分类Dev

Newtonsoft.Json 使用“添加”反序列化集合

来自分类Dev

如何使用 Newtonsoft.Json 反序列化?

来自分类Dev

您如何使用newtonsoft JSON反序列化器反序列化Geopoint?

来自分类Dev

newtonsoft json反序列化错误处理:部分反序列化

来自分类Dev

使用Newtonsoft Json.Net反序列化数组对象

来自分类Dev

如何反序列化Newtonsoft Json.NET引用以分离单独的实例

来自分类Dev

具有继承功能的反序列化Newtonsoft JSON.NET无法正常工作

Related 相关文章

  1. 1

    Newtonsoft json.net反序列化NullReferenceException

  2. 2

    Newtonsoft JSON.NET反序列化错误

  3. 3

    使用 NewtonSoft 反序列化 JSON

  4. 4

    使用Newtonsoft.Json反序列化Json数组

  5. 5

    反序列化Json Array Newtonsoft.Json

  6. 6

    在C#中使用Newtonsoft Json.NET反序列化JSON

  7. 7

    Newtonsoft JSON.net反序列化错误,其中JSON中的字段更改顺序

  8. 8

    在C#中使用Newtonsoft Json.NET反序列化JSON

  9. 9

    JSON反序列化(NewtonSoft JSON.NET)到XML失败

  10. 10

    使用newtonsoft.json反序列化List <AbstractClass>

  11. 11

    newtonsoft.json反序列化逻辑

  12. 12

    Newtonsoft JSON使用HttpWebResponse反序列化

  13. 13

    Newtonsoft JSON反序列化-密钥作为属性

  14. 14

    newtonsoft json反序列化的问题(崩溃)

  15. 15

    用newtonsoft json问题反序列化列表

  16. 16

    用Newtonsoft.Json枚举反序列化

  17. 17

    newtonsoft json模式反序列化ValidationError

  18. 18

    Newtonsoft JSON反序列化以从列表中键入

  19. 19

    Newtonsoft JSON使用HttpWebResponse反序列化

  20. 20

    用newtonsoft反序列化json数组

  21. 21

    用Newtonsoft.Json反序列化

  22. 22

    Newtonsoft JSON Schema忽略用于反序列化的验证

  23. 23

    Newtonsoft.Json 使用“添加”反序列化集合

  24. 24

    如何使用 Newtonsoft.Json 反序列化?

  25. 25

    您如何使用newtonsoft JSON反序列化器反序列化Geopoint?

  26. 26

    newtonsoft json反序列化错误处理:部分反序列化

  27. 27

    使用Newtonsoft Json.Net反序列化数组对象

  28. 28

    如何反序列化Newtonsoft Json.NET引用以分离单独的实例

  29. 29

    具有继承功能的反序列化Newtonsoft JSON.NET无法正常工作

热门标签

归档